Can enyone explain to me how to remove firebase Eventlistener/childEventlistener in Android realtime database and how to detect if the listener is successfully removed? I use below code but not sure this is ok or not
class dummy AppCompatActivity() {
lateinit var dbRaceRef: DatabaseReference
lateinit var mListener: ValueEventListener
oncreate{
mListener = dbRaceRef.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
// do something
}
}
})
}
ondestroy{
dbRaceRef.removeEventListener(mListener)
}
}
And what happens if I didn't remove my listener. When it will be destroyed automatically? Can we somehow check what are the active listeners in my database?
Firebase Realtime Database listeners are not automatically removed. They only stop when you explicitly remove them, or when the app gets killed. There is no API to get a list of the active listeners, so you will have to track those yourself.
The earliest you should usually connect your listeners is in onStart
, in which case you should remove them in the onStop
of the activity.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments