Disable the first query snapshot when adding a snapshotListener

后端 未结 2 1715
梦谈多话
梦谈多话 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.

提交回复
热议问题