10

For receiving data from Firebase Realtime Database I need to register listener:

objectReference.addValueEventListener(valueEventListener);

What is correct way to remove (unregister) this listener?

tse
  • 5,769
  • 6
  • 38
  • 58

3 Answers3

18

The correct way to remove a listener is to remove it accordingly to the life-cycle of your activity using this line of code:

databaseReference.removeEventListener(valueEventListener);

Note that, if you have added the listener in onStart you have to remove it in onStop. If you have added the listener in onResume you have to remove it in onPause. If you have added the listener in onCreate you have to remove it in onDestroy.

But remember onDestroy is not always called.

Edit:

The above databaseReference object can be any object of type DatabaseReference. For instance, if you are listening to real-time updates to a node called users:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("users");

The code to remove the lister should look like this:

usersRef.removeEventListener(valueEventListener);

And here is the correct import:

import com.google.firebase.database.DatabaseReference;
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yep, thank you. I was confused a little because names of adding and removing functions are not consistent. – tse Nov 07 '17 at 14:19
  • You can see here on info about activity life cycle : and when to add and destroy based on your needs : https://stackoverflow.com/a/61299029/3904109 – DragonFire Apr 19 '20 at 03:49
  • removeEventListener deprecated? I don't have this option for FirebaseDatabase.instance.ref(myPath) – Dpedrinha Aug 01 '22 at 23:01
  • @Dpedrinha The [removeEventListener(ValueEventListener listener)](https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query#removeEventListener(com.google.firebase.database.ValueEventListener)) method exists inside the [Query](https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query) class, and it's **not** deprecated. – Alex Mamo Aug 02 '22 at 05:58
  • @AlexMamo can you please edit your answer and add the code to get databaseReference? Because my databaseReference doesn't show the removeEventListener option. What am I missing? – Dpedrinha Aug 03 '22 at 15:36
  • 1
    @Dpedrinha Please check my updated answer. Don't also forget to add the correct import. – Alex Mamo Aug 03 '22 at 15:46
  • @AlexMamo It seems I'm using the wrong import then. My code is exactly the same and it doesn't autocomplete removeEventListener. Can you please add your import to the answer? – Dpedrinha Aug 04 '22 at 16:42
  • 1
    @Dpedrinha Sure, please check my updated answer. Please also make sure to have the correct dependency added. – Alex Mamo Aug 05 '22 at 07:52
2

Its better to check whether listener is null or has an object, because if the listener object is null there will be a runtime error

if(valueEventListener!=null){
  databaseReference.removeEventListener(valueEventListener);
}
Lasitha Lakmal
  • 486
  • 4
  • 17
-1

You can also do it like this:

  componentWillUnmount() {
    firebase.database().ref('example').child(this.state.somethingDyamic).off('value');
  };

  doSomething() {
    firebase.database().ref('example').child(this.state.somethingDyamic).on('value', (snapshot) => {
      ...
   });
  }
Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
  • What if the state changes in the lifecycle of the component? Then a different child will be unbound.. – TrySpace Sep 05 '19 at 14:00