I have implemented RecyclerView
with FirebaseUI-Android library.
My RecyclerView
realtime data change well once i use that FirebaseRecyclerAdapter
In Collection that data document have field type as Boolean , Integer, Reference.
I want use that Reference to get data in populateViewHolder
with addSnapshotListener
.
Help me! Here is my code:
FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
Conv.class,
R.layout.users_single_layout,
ConvViewHolder.class,
conversationQuery
) {
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {
final String list_user_id = getRef(i).getKey();
final DocumentReference docRef = db.collection("cities").document(list_user_id);
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
if (snapshot != null && snapshot.exists()) {
Log.d(TAG, "Current data: " + snapshot.getData());
} else {
Log.d(TAG, "Current data: null");
}
}
});
}
};
mConvList.setAdapter(firebaseConvAdapter);
Firebase saying if you add addSnapshotListener then must to remove it once no need for that Detach a listener
When you are no longer interested in listening to your data, you must detach your listener so that your event callbacks stop getting called. This allows the client to stop using bandwidth to receive updates. You can use the unsubscribe function on
onSnapshot()
to stop listening to updates.