0

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?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Surendar
  • 239
  • 2
  • 12

3 Answers3

2

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for clarifying, Is my mentioned code correct for removing the listener?( I personally think that there is lot of work going on to remove listener, meaning intializing both ref and listener, then keep in memory then, remove on destroy). and how to check if the listener is successfully removed. – Surendar Dec 30 '19 at 17:06
  • Yes, that should work. See https://firebase.google.com/docs/database/android/read-and-write#detach_listeners – Frank van Puffelen Dec 30 '19 at 17:10
  • Anyway to verify that the listener is removed ? – Surendar Dec 30 '19 at 18:14
  • Sure. Make a change in the database and see if the app still gets updated. If it does, the trigger was not removed. If it not longer gets updated, the trigger was removed. – Frank van Puffelen Dec 30 '19 at 18:52
0

If you want use just one time get values you can use as follows.

private val databaseReference = FirebaseDatabase.getInstance().getReference("___")

databaseReference.addValueEventListener(object : ValueEventListener {
    override fun onCancelled(p0: DatabaseError) {
        databaseReference.removeEventListener(this)
    }
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        databaseReference.removeEventListener(this)
    }
})

Or you can use addListenerForSingleValueEvent

But if you are didn't remove listener and you changing value depending on listener you can make infinite loop.You need to be careful.

utkukutlu
  • 400
  • 1
  • 4
  • 5
  • No i wanted to use valueeventlistener only, i just want to remove it when activity destroys. Can you tell me how to remove my listener. – Surendar Dec 30 '19 at 13:32
0

If you need to receive data one time you need to use addListenerForSingleValueEvent(...) instead of addValueEventListener(...). Then onDataChange() will return only one time

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • No I wanted to listen for changes from database while my activitity is live, but my firebase recomends to delete this value eventlistener after use. I don't know how to do that – Surendar Dec 30 '19 at 13:52