Ronnie L Ronnie L - 2 months ago 13
Swift Question

How to retrieve a key value for a Firebase 3.0 item / Swift

I am new to Firebase 3.0 and coding and am having trouble retrieving a specific node ID and writing to it. In the example below, I just created the user and now want to add that user to a specific facility. How would I search for the facility named "Upper Paw" and then add the user to it

Current:

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


Goal:

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

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


Here is my code to pull up the values in facilities (which pulls up the all facility data, but unsure how to query and find the facility for Upper Paw and write additional data to it:

let facRef = rootRef.child("facilities")

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

print(snapshot.value)

Answer
let facRef = rootRef.child("facilities")

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

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

    print(snapshot.value)

See the Firebase documentation on querying for more information.

For learning how to modify the data, see the Firebase documentation on saving data. Based on that your block could be:

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