Reputation: 548
I have a dating kind of app (without chat support) in which I am showing list of all the profiles that matches certain criteria to user. I have used realtime snapshot listener for this.
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
if (snapshot != null && !snapshot.isEmpty()) {
List<FeedDetails> feedDetailsList = new ArrayList<>();
for (DocumentSnapshot document : snapshot.getDocuments()) {
FeedDetails feedDetails = document.toObject(FeedDetails.class);
feedDetailsList.add(feedDetails);
}
feedItemAdapter.updateData(feedDetailsList);
}
}
});
In individual profile doc, I have 10-12 field with one online/offline info field. So lets say if no other field changed but online/offline status changed for some profiles then listener is going to read that document again. Is there any way to cut down those reads?
Upvotes: 1
Views: 844
Reputation: 138824
I have a 10-12 field with one online/offline info field. So let's say if no other field changed but online/offline status changed for some profiles then the listener is going to read that document again.
There is no way in Cloud Firestore in which you can listen only to a set of properties of a document and exclude others. It's the entire document or nothing. So if this field online=true
is changed into online=false
, then you will get the entire document.
Cloud Firestore listeners fire on the document level and always return complete documents. Unfortunately, there is no way to request only a part of the document with the client-side SDK, although this option does exist in the server-side SDK's select() method.
If you don't want to get notified for specific fields, consider adding an extra collection with documents that will contain only those fields. So create that additional collection where each document just contains the data you don't need. In this way, you won't be notified for online/offline changes.
Upvotes: 2