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);
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With