Firebase数据库参考在插入新数据后不返回更新的数据快照

时间:2019-04-17 09:45:22

标签: android firebase firebase-realtime-database kotlin

我在我的项目中集成了Firebase实时数据库。当活动在那时打开时,我正在检查用户是否已在数据库中签入,并且为此,我正在从checkIsAlreadyCheckIn(empDataRef)调用onCreate()方法。检查以下代码。

class EmpProfileActivity : BaseActivity<EmpProfileActivityBinding, EmpViewModel>() {

    private val timeSdf = SimpleDateFormat("hh:mm a", Locale.getDefault())
    private val dateSdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

    private lateinit var isCheckIn: String
    private lateinit var checkInTime: String
    private lateinit var checkOutTime: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = EmpViewModel()
        mBinding.empProfileViewModel = mViewModel

        val empDataRef: DatabaseReference =
            mDatabase!!.reference.child(FirebaseKey.NodeEmpCheckInOut)
                .child(dateSdf.format(Date())).child(userSession!!.getUserId())

        checkIsAlreadyCheckIn(empDataRef)

        mBinding.txtCheckIn.setOnClickListener {
            when (isCheckIn) {
                "0" -> {
                    empDataRef.child(FirebaseKey.checkIn).setValue(timeSdf.format(Date()))
                    empDataRef.child(FirebaseKey.checkOut).setValue("")
                    empDataRef.child(FirebaseKey.todayDate).setValue(dateSdf.format(Date()))
                    empDataRef.child(FirebaseKey.isCheckIn).setValue("1")
                }
                else -> showAlert("Something went wrong, Please contact your admin")
            }
        }
    }


    private fun checkIsAlreadyCheckIn(empDataRef: DatabaseReference) {

        empDataRef.addValueEventListener(object : ValueEventListener {
            override fun onCancelled(databaseError: DatabaseError) {
                Log.e("Error:", databaseError.message)
            }

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for(data:DataSnapshot in dataSnapshot.children){
                    //------------>aftet click that view error will come here 
                    isCheckIn = dataSnapshot.child(FirebaseKey.isCheckIn).value as String 
                }
            }
        })
    }
}

当我点击 mBinding.txtCheckIn 视图时,它将成功在fireabse DB中插入数据,但是在addValueEventListenercheckIsAlreadyCheckIn()方法收到回叫后,将向我发送以下错误消息

kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String
        at com.intro.ui.profile.EmpProfileActivity$checkIsAlreadyCheckIn$1.onDataChange(EmpProfileActivity.kt:124)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6165)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

我已经在kotlin文档中检查了kotlin.TypeCastException: null cannot be cast to non-null type kotlin,我认为这意味着dataSnapshot值为null,因此我们无法隐瞒String。

问题::一旦数据插入Firebase中并插入addEventValueListener中的回调/触发器,如何更新/接收数据?

1 个答案:

答案 0 :(得分:2)

toString()的使用非常灵活-尝试以下操作:-

isCheckIn = dataSnapshot.child(FirebaseKey.isCheckIn).value.toString()

P.S:-即使其null-也会得到-"null"(作为字符串)。然后,您可以相应地添加支票。