如何使用FirebaseFirerestore监听实时更新,SwiftUI [已关闭]

goqiplq2  于 2023-06-24  发布在  Swift
关注(0)|答案(1)|浏览(131)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

1年前关闭。
Improve this question
我用SwiftUI-FirebaseFirerestore制作ChatApp。我同时运行2个模拟器,如果用户A向用户B发送消息,我必须重新加载用户B的界面以显示。我希望它是自动的,并立即显示,那么该怎么做?

b4lqfgs4

b4lqfgs41#

您要查找的内容称为快照侦听器。实际上,它的处理方式与任何其他文档相同,但它有一个闭包,允许您响应特定文档上的更新事件。它的一般用法是这样的。

db.collection("cities").document("SF")
.addSnapshotListener { documentSnapshot, error in
  guard let document = documentSnapshot else {
    print("Error fetching document: \(error!)")
    return
  }
  guard let data = document.data() else {
    print("Document data was empty.")
    return
  }
  print("Current data: \(data)")
}

您应该能够查看文档快照以确定数据中的任何更改,然后响应这些更改。在本例中,您将重新加载视图。
https://firebase.google.com/docs/firestore/query-data/listen

相关问题