Should you always delete a Firebase listener?

If I use a listener in action as follows:

// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        String value = dataSnapshot.getValue(String.class);
        Log.d(TAG, "Value is: " + value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});

Attaching an anonymous listener (an event that is not bound to a variable), do I still need to delete it?

* I install this on onStart()and need it to run before onStop()/onDestroy()

When do I need to remove a listener?

+4
source share
1 answer

If you want the listener to work while the asset is active, you can disable the listener by calling the removeEventListener () method on the Firebase database database. If you attach the listener in your onStart (), you must disconnect in onStop ().

@Override
protected void onStop() {
    super.onStop();

    //...
    myRef.removeEventListener();
}
+1
source

Source: https://habr.com/ru/post/1660931/


All Articles