代码之家  ›  专栏  ›  技术社区  ›  Abdulmoiz Ahmer

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

  •  1
  • Abdulmoiz Ahmer  · 技术社区  · 6 年前

    假设我的firebase realtime数据库中有子名用户,而子名用户又有子名,这些子名用户是类似于“christialliz@gmail.com”的电子邮件,那么子名用户的电子邮件中还有子名、年龄和国家。

    我在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("")
        }
    

    当我测试后改变国家的价值,应用程序崩溃,原因是这个价值![姓名]。现在我知道childChanged事件只返回在我的测试场景“country”中更改的子级,所以其他值不在快照中,但我想检查哪个值是更新的名称、年龄或国家。如何修改上面的代码来实现这一点。

    1 回复  |  直到 6 年前
        1
  •  1
  •   excitedmicrobe    6 年前

    使用 hasChild 检查子项是否存在:

    if snapshot.hasChild("country"){
          print("found")
        } else {
          print("not found")
    }