Ios AddSnapShotListener正在一个实例中重复文档读取

Ios AddSnapShotListener正在一个实例中重复文档读取,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,当我使用addSnapshotListener进行实时更新时,文档会重复出现,但当使用getDocuments()时,文档只会重复出现一次,我需要使用addSnapshotListener但不想重复文档读取,请协助我在使用快照侦听器时出错的地方 我正在使用Swift iOS中的Firestore数据库。下面是我正在使用的代码 使用addSnapShotListener()编码: 使用getDocuments()编码: 您可能希望只处理快照之间的更改。为此,您需要循环使用而不是,如以下文档所示:

当我使用addSnapshotListener进行实时更新时,文档会重复出现,但当使用
getDocuments()
时,文档只会重复出现一次,我需要使用
addSnapshotListener
但不想重复文档读取,请协助我在使用快照侦听器时出错的地方

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

使用addSnapShotListener()编码:

使用getDocuments()编码:


您可能希望只处理快照之间的更改。为此,您需要循环使用
而不是
,如以下文档所示:


最初,您的侦听器将被调用为每个现有文档添加
diff.type==.added
,然后当有更改时,它将被调用为
type
s的正确组合。

您可能希望只处理快照之间的更改。为此,您需要循环使用
而不是
,如以下文档所示:


最初,您的侦听器将被调用,并为每个现有文档添加
diff.type==,然后,当有更改时,它将被调用为正确的
type
s组合。

我建议在
snapshot
可用时清除
comments
数组,然后重新填充它将始终保留一份数据副本,而不复制它们。当前,您正在将快照中的项目添加到
注释
数组,该数组将在
注释ref时添加重复的项目。当检测到更改时,将调用getDocuments
。我建议在
快照
可用时清除
注释
数组,然后重新填充它将始终保留一份数据副本,而不进行任何更改复制它们。当前,您正在将快照中的项目附加到
注释
数组,该数组将在
注释ref时添加重复的项目。当检测到更改时,将调用getDocuments
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()
                }
            }
        }
    }
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()
                }
            }
        }
    }
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())")
            }
        }
    }