Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I remove after using "addListenerForSingleValueEvent"

As the document says, listener for SingleValueEvent only run one time.
Then is it unnecessary to remove listener after using it like this?

final Query query = getChatsRef().limitToLast(20);
query.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        query.removeEventListener(this);         
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        query.removeEventListener(this); 
    }
});
like image 744
wonsuc Avatar asked Feb 27 '17 18:02

wonsuc


1 Answers

No. Removing the listener as you do in your snippet of code is not needed.

The only reason you might want to remove a once listener, is when the listener hasn't fired yet. The only time I can see that happening is when you're not connected to the Firebase servers and the location you're inspecting is not cached. That should be a fairly small number of cases.

like image 176
Frank van Puffelen Avatar answered Oct 02 '22 21:10

Frank van Puffelen