首页 > 解决方案 > Firebase 实时数据库的节点回调中没有数据?

问题描述

我试图找到一种方法来处理数据库状态(零状态)中的任何数据。

我的代码是:

Database.database()
    .reference(withPath: "My_Path")
    .child("My_Inner_Path")
    .observeSingleEvent(of: .value) { (snapshot) in
        // Callback with data at the observing node.
}

但如果节点为空或尚不存在,则上述代码不会被回调。我需要这样做,以便我可以隐藏我的指标并显示零状态或触发一些其他方法以在节点状态下没有数据。提前致谢。

标签: iosswiftfirebasefirebase-realtime-database

解决方案


以下是我的问题的解决方案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
    }    
}

推荐阅读