swift - 区分记录更改和 Firebase 中删除的数据
问题描述
使用以下代码,我试图观察记录何时更改或何时被删除。不过,缺少快照似乎并不表示删除。有什么方法可以区分这两种情况吗?
FireRef.child("dataRecords").child(dataRecord).observe(.value, with: { (snapshot) in
if snapshot.exists() {
let snapshot = snapshot.value as! [String: AnyObject]
print("record has been modified: \(snapshot)")
} else {
print("record has been deleted")
}
})
解决方案
如果我理解您的问题,您可以在Firebase 文档中找到答案。
// Listen for new comments in the Firebase database
commentsRef.observe(.childAdded, with: { (snapshot) -> Void in
self.comments.append(snapshot)
self.tableView.insertRows(at: [IndexPath(row: self.comments.count-1, section: self.kSectionComments)], with: UITableViewRowAnimation.automatic)
})
// Listen for deleted comments in the Firebase database
commentsRef.observe(.childRemoved, with: { (snapshot) -> Void in
let index = self.indexOfMessage(snapshot)
self.comments.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: self.kSectionComments)], with: UITableViewRowAnimation.automatic)
})
你也可以commentsRef.observe(.childChanged, .....)
使用
推荐阅读
- postman - 如何让邮递员测试失败 - 我的总是通过?
- python - Pybind11嵌入式模块导入时不运行
- java - 用 ifPresent 和 orElse 替换 isPresent
- python - 在 Python 的 csv.reader 中删除神秘的双标签
- javascript - 用纯 Jest 库(无酶)测试反应形式不起作用
- git - 解决 Git 中的冲突
- c# - 编写一个方法的签名,该方法将获取特定配方的配方中的成分列表。并非所有食谱都为人所知
- sql-server - Windows 10 更新 2004 中断 SQL 连接
- python-2.7 - 数据框和系列的元素乘积以及组内的滚动总和
- angular - 角度 9,我可以使用 canDeactivate 路由到其他地方吗?