Firestore realtime updates documentation here
Here is the comment you can find in the documentation.
Important: The first query snapshot cont
AtomicBoolean isFirstListener = new AtomicBoolean(true);
commentListener = getLectureCommentsCollecReference(courseId, lectureId)
.addSnapshotListener((queryDocumentSnapshots, e) -> {
if (isFirstListener.get()) {
isFirstListener.set(false);
//TODO Handle the entire list.
return;
}
for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
sendCommentAddedEvent(DataParser.parseComment(dc));
case MODIFIED:
sendCommentUpdatedEvent(DataParser.parseComment(dc));
break;
}
}
});
This is one way of doing it. I use this inside a comment feature for listening to new comment Added as well as if comments are modified.
There is no way to suppress getting the initial data.
Depending on the use-case, you may want to attach your listener to a subset of the data. E.g. if you have an append-only scenario (such as chat), you can start listening for data that was modified after "now".