ios - 使用 swift 4 从 ios 监听 Firestore 中的实时更新
问题描述
我正在尝试收听集合中特定文档的创建,然后在发生更改后打印文档数据。仅当关闭应用程序或创建文档时,此侦听才应停止。
我尝试使用文档中的代码,但根据经验发现,即使文档不存在,在使用 print 语句执行一次后监听也会停止 - “文档数据为空”。
let uid = "DocumentID"
let db = Firestore.firestore()
db.collection("MyCollection").document(uid!)
.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
}
let data = document.data()
print("Current data: \(String(describing: data))")
我需要的代码可以等到在集合中创建文档,然后在其中打印文档 ID 先前已知的数据。
解决方案
推荐阅读
- php - symfony 2.8 谷歌驱动 api V2
- python - 文件中的空行时中断
- php - 在 Lumen 控制器中验证电子邮件并返回 JSON
- pine-script - VWMA 手动计算和 TradingView 计算不匹配
- python - 修改对象内的 JSON 键/值对
- h2o - 带有 Radeon Rx Vega GPU 的 Ubuntu 16.04 上的 H2o Deepwater 后端
- android - 直接从应用程序发送推送通知
- keras - 如何向 Keras 制作的 ML Engine 模型发送 POST 请求?
- java - 带有 Spring Boot 的 Spring 标签库问题
- scala - 如何在scala中收集分页结果