ios - Firebase 实时数据库的节点回调中没有数据?
问题描述
我试图找到一种方法来处理数据库状态(零状态)中的任何数据。
我的代码是:
Database.database()
.reference(withPath: "My_Path")
.child("My_Inner_Path")
.observeSingleEvent(of: .value) { (snapshot) in
// Callback with data at the observing node.
}
但如果节点为空或尚不存在,则上述代码不会被回调。我需要这样做,以便我可以隐藏我的指标并显示零状态或触发一些其他方法以在节点状态下没有数据。提前致谢。
解决方案
以下是我的问题的解决方案Swift
。
Database.database()
.reference(withPath: "My_Path")
.child("My_Inner_Path")
.observeSingleEvent(of: .value) { (snapshot) in
guard snapshot.exists() else {
// The node doesn't exist.
return
}
// OR
guard snapshot.childrenCount != 0 else {
// No children exist.
return
}
}
推荐阅读
- php - 如何修复 Google Sheets API 的“找不到类”错误?
- php - 严格标准:只有变量应该在 /main_dir/sub_dir/backup.php 中通过引用传递
- ruby-on-rails - rails belongs_to 可选,但如果通过则检查存在
- node.js - filebeat json 记录到 elasticsearch 最大大小
- haskell - 自动创建检测和类型组件的谓词?
- mysql - 根据 MariaDB 语法检查 SQL 语法
- python - 检测何时单击“X”按钮
- c - 我使用链表(不带数组)创建了一个堆栈,但我的 pop 函数不起作用
- javascript - 如何以角度引用外部 javascriptfile?
- javascript - 根据值对对象数组进行排序