Disable the first query snapshot when adding a snapshotListener

后端 未结 2 1695
梦谈多话
梦谈多话 2021-01-16 21:32

Firestore realtime updates documentation here

Here is the comment you can find in the documentation.

Important: The first query snapshot cont

相关标签:
2条回答
  • 2021-01-16 22:02
    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.

    0 讨论(0)
  • 2021-01-16 22:05

    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".

    0 讨论(0)
提交回复
热议问题