假设我的 Firebase 实时数据库有子名称 Users,它还有子子,如“christianLiz@gmail.com”这样的电子邮件,子子电子邮件还有名为 name、age 和 country 的子子。
我在 child 上添加了一个 observeEvent,代码片段看起来像这样。
ref.child("USERS").child("christianLiz@gmail,com").observe(.childChanged, with: { snapshot in
if snapshot.exists(){
let value = snapshot.value as? NSDictionary
if value!["name"] != nil{
print("here")
}else{
print("not here")
}
}){ (error) in
print("")
}
当我在更改国家/地区的值后进行测试时,应用程序崩溃,原因是这个值!["name"]。现在我知道 childChanged 事件只返回在我的测试场景“国家”中更改的 child ,所以其他值不在快照中,但我想检查哪个是更新的名称或年龄或国家。我怎样才能修改上面的代码来实现这一点。
最佳答案
使用hasChild
来检查 child 是否存在:
if snapshot.hasChild("country"){
print("found")
} else {
print("not found")
}
关于ios - 如何知道在 swift 4 中实时 firebase 的 childChanged 事件类型中更新了哪个子子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841633/