ios - AddSnapShotListener 在一个实例中重复读取文档

标签 ios swift firebase google-cloud-firestore

当我使用 addSnapshotListener 进行实时更新时,文档会重复,但情况并非如此,但是当使用 getDocuments() 时,文档只重复一次,我需要使用 addSnaphotListener 但不想重复文档读取,请协助我使用快照监听器的错误。

我在 Swift iOS 中使用 Firestore 数据库。下面是我正在使用的代码

带有 addSnapShotListener() 的代码:

func getComments() {

      //print(postId + "received")
        let commentsRef = Firestore.firestore().collection("posts").document(postId).collection("comments")

        commentsRef.addSnapshotListener { (snapshot, error) in

            if let error = error {

                print(error.localizedDescription)

            } else {

                if let snapshot = snapshot {

                    for document in snapshot.documents {

                      // self.length = snapshot.count


                        let data = document.data()
                        let username = data["comment_author_username"] as? String ?? ""
                        let comment = data["comment_author_comment"] as? String ?? ""
                        let spinnerC = data["comment_author_spinnerC"] as? String ?? ""
                        let fullname = data["comment_author_fullname"] as? String ?? ""
                        let email = data["comment_author_email"] as? String ?? ""
                        let commentUserImageUrl = data["comment_user_image"] as? String ?? ""
                        let commentuser_id = data["comment_author_id"] as? String ??  ""
                        self.checkl1value = data["l1"] as? Bool


                        let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC, _commentAuthorId:commentuser_id, _checkl1value: self.checkl1value)

                        self.comments.append(newComment)
                    //   print(self.length!)

                    }
                   self.tableView.reloadData()
                }
            }
        }
    }

使用 getDocuments() 编写代码:

func getComments() {

      //print(postId + "received")
        let commentsRef = Firestore.firestore().collection("posts").document(postId).collection("comments")

        commentsRef.getDocuments { (snapshot, error) in

            if let error = error {

                print(error.localizedDescription)

            } else {

                if let snapshot = snapshot {

                    for document in snapshot.documents {

                      // self.length = snapshot.count


                        let data = document.data()
                        let username = data["comment_author_username"] as? String ?? ""
                        let comment = data["comment_author_comment"] as? String ?? ""
                        let spinnerC = data["comment_author_spinnerC"] as? String ?? ""
                        let fullname = data["comment_author_fullname"] as? String ?? ""
                        let email = data["comment_author_email"] as? String ?? ""
                        let commentUserImageUrl = data["comment_user_image"] as? String ?? ""
                        let commentuser_id = data["comment_author_id"] as? String ??  ""
                        self.checkl1value = data["l1"] as? Bool


                        let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC, _commentAuthorId:commentuser_id, _checkl1value: self.checkl1value)

                        self.comments.append(newComment)
                    //   print(self.length!)

                    }
                   self.tableView.reloadData()
                }
            }
        }
    }

最佳答案

您可能只想处理快照之间的更改。为此,您需要遍历 而不是 ,如 viewing changes between snapshots 上的文档所示。 :

db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
        }
        snapshot.documentChanges.forEach { diff in
            if (diff.type == .added) {
                print("New city: \(diff.document.data())")
            }
            if (diff.type == .modified) {
                print("Modified city: \(diff.document.data())")
            }
            if (diff.type == .removed) {
                print("Removed city: \(diff.document.data())")
            }
        }
    }

最初,您的监听器将通过 diff.type == .added 为每个现有文档调用,然后当有更改时,它将通过 type< 的正确组合调用s.

关于ios - AddSnapShotListener 在一个实例中重复读取文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58967357/

相关文章:

ios - Mac Catalyst - 向 UITableView/UIScrollView 添加单击 + 拖动手势

ios - UIKitCore : _UIAnalyticsGatherMultitouchAnalytics 崩溃

ios - 如果核心数据中没有其他与该对象的关系,如何删除对象?

安卓通知 : onMessageReceived not called

ios - Xamarin.iOS 每天自动更改本地通知标题

swift - 去初始化、 self 和闭包。

swift - 查看pdf文档

ios - 从 UILabel 触发 PickerView 使其以模态方式显示在屏幕底部,而不是隐藏/取消隐藏它

javascript - 从 firebase 函数返回

javascript - 将 Firestore 可观察对象转换到自定义对象