How to set addSnapshotListener and remove in populateViewHolder in RecyclerView Item?
Solution 1:
To achieve this, you need to use a EventListener<DocumentSnapshot>
like this:
EventListener<DocumentSnapshot> eventListener = new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
if (snapshot != null && snapshot.exists()) {
//Do what you need to do
}
}
};
Declare a global ListenerRegistration listenerRegistration;
variable and add the SnapshotListener in the place where is needed like this:
if (listenerRegistration == null ) {
listenerRegistration = yourRef.addSnapshotListener(eventListener);
}
To remove the listener, just use the following lines of code, in your onStop()
method:
@Override
protected void onStop() {
if (listenerRegistration != null) {
listenerRegistration.remove();
}
}
Also, don't forget to add it again once your onStart()
method is called.
@Override
protected void onStart() {
super.onStart();
listenerRegistration = yourRef.addSnapshotListener(eventListener);
}
When you are calling addSnapshotListener
for listening to realtime updates, it means that you attach a listener that gets called for every change that takes place in your database. So this is happening also when your app is closed, that's why it's mandatory to detach the listeners before the activity gets destroyed.
If in your app you don't need to get the data in realtime, then you can simply use a get() call directly on the reference, which just reads the document only once. Since it only reads once, there is no listener to be removed. This is the correspondent of addListenerForSingleValueEvent()
used in the Firebase realtime database.
There is also a more elegant way for removing the listener which is to pass the activity as first the argument in the addSnapshotListener() method, so Firestore can clean up the listeners automatically when the activity is stopped.
ListenerRegistration lg = yourDocumentRef
.addSnapshotListener(YourActivity.this, eventListener);
Solution 2:
There is also a more elegant way for removing the listener which is to pass the activity as first the argument in the addSnapshotListener() method, so Firestore can clean up the listeners automatically when the activity is stopped. Blockquote
It was very important. I did not search for "elegant way", but it was hint why my listener stops sometimes. I removed activity field, and now listener not stopping without my direct instruction in case of activity state changing.