gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:24:56 25 4
gpt4 key购买 nike

当我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com