如何检索Firebase 3.0项/ Swift的键值

时间:2016-09-09 19:47:32

标签: swift firebase firebase-realtime-database

我是Firebase 3.0和编码新手,无法检索特定节点ID并写入其中。在下面的示例中,我刚刚创建了用户,现在想要将该用户添加到特定设施。我如何搜索名为" Upper Paw"然后将用户添加到其中

当前:

application  
    -facilities  
        -KRFGFn7TzKR8D0bMLW  
            facName: "Upper Paw"  
            facPhone: "5122223333"  
    -users  
        -MqrvHRBTRcPzrvAOdkklBzeFW7E2  
            firstName: "Ronnie"  
            lastName: "Last"  

目标:

application   
    -facilities  
        -KRFGFn7TzKR8D0bMLW  
            facName: "Upper Paw"  
            facPhone: "5122223333"
            users:   
                -MqrvHRBTRcPzrvAOdkklBzeFW7E2  
                    firstName: "Ronnie"  
                    lastName: "Last"  
    -users  

        -MqrvHRBTRcPzrvAOdkklBzeFW7E2  
            firstName: "Ronnie"  
            lastName: "Last"  

这是我的代码来提取设施中的值(它提取所有设施数据,但不确定如何查询和找到上爪的设施并向其写入其他数据:

let facRef = rootRef.child("facilities")

            facRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) in

             print(snapshot.value)

2 个答案:

答案 0 :(得分:1)

let facRef = rootRef.child("facilities")

let faqQuery = facRef. queryOrderedByChild("facName"). queryEqualToValue("Upper Paw")

faqQuery.observeSingleEventOfType(.ChildAdded, withBlock: { (snapshot) in

    print(snapshot.value)

有关详细信息,请参阅Firebase documentation on querying

要了解如何修改数据,请参阅Firebase documentation on saving data。基于你的块可能是:

 snapshot.ref.child("facMember").setValue("Ronnie L")

答案 1 :(得分:1)

    let ref = FIRDatabase.database().reference(fromURL: "DATABASE_URL")
    let usersRef = ref.child("users")
    usersRef.queryOrdered(byChild: "facName").queryEqual(toValue: "Upper Paw").observeSingleEvent(of: .value, with: { (snapshot) in
    print(snapshot)
   })