我有一个存在的ref,我使用observeEventType
来查询数据。但是由于用户删除它,ref可能没有数据。我使用snapshot.exists()
进行测试。在snapshot.exists()
以下的情况下将返回false/no
。由于它是假的,我想做别的事情,但代码永远不会运行当snapshot.exists()返回false时该怎么办?
当snapshot.exists()返回false/no时,我该怎么做点别的?
//there is no data at levelTwo so there's nothing to observe
let levelTwoRef = dbRef.child("players").child("uid").child("levelTwo")
levelTwoRef.observeEventType(.ChildAdded, withBlock: {
(snapshot) in
if snapshot.exists(){
if let dict = snapshot.value as? [String:AnyObject]{
let power = dict["power"] as? String
let score = dict["score"] as? String
}
//this will never run because the data has been deleted
} else{
do something else as an alternative //why isn't this running??
}
})
用您的数据库JSON结构更新您的问题(实际上,因为文本不是图像!)。 – Dravidian
这不是您的JSON结构,您可以通过点击Firebase数据库控制台中的EXPORT JSON获取JSON结构,将其最小化为最相关的部分,然后发布。 – Dravidian
@Dravidian你教会了我一些新的东西。我从来不知道这一点。我会做几个。谢谢! –