ios - 如何知道在 swift 4 中实时 firebase 的 childChanged 事件类型中更新了哪个子子

标签 ios swift firebase firebase-realtime-database

假设我的 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/

相关文章:

ios - 发电机 ios

ios - iOS模拟器“找不到SDK”,并且“可能需要重新安装”

ios - 打开 iOS 应用程序后,未调用 "continue userActivity:"方法 - Firebase 动态链接

ios - 解析 becomeInBackground 将 currentUser.sessionToken 设置为 null

SwiftUI 从列表中向上传递 View 层次结构的数据

swift - 如何在 Swift 结果生成器中恢复类型泛型类型推断?

ios - TableView/Screen 的 AutoLayout UITableViewCell 高度倍数

firebase - 在 Flutter 中,我们如何使用 Firebase Messaging onBackgroundMessage 创建通知,使用 flutter_local_notifications?

ios - Firebase 通知在 iOS 11 中不起作用

java - 从 firebase 获取列表时,特定值仅显示在一个 recyclerview 项目中,但不会显示在列表中显示的其他项目中