Updating a Realtime Database object in Firebase iOS SDK

Viewed 394

I have a database that looks like this: enter image description here

I want to update let's say the second user, the one with uid 5p3XT8TVFbVYNmFt0Kbu1ticaUT2, and change its name to "James". Using the iOS SDK I can do:

let ref = Database.database().reference()
let id = 5p3XT8TVFbVYNmFt0Kbu1ticaUT2
let databaseQuery = ref.child("users").queryOrdered(byChild: "uid").queryEqual(toValue: id)

In order to query the database so it will filter out all users apart from the one I want to change. But this query will now point to a filtered list of users which contains only the user I want to update, not to the user itself. So if I try

databaseQuery.ref.updateChildValues(...)

The database will look like this:

enter image description here

I can't understand from the api reference how to get straight to the queried child. How do I do that?

1 Answers

You can set value to be changed in the values dictionary

func updateValues(){
    let ref = Database.database().reference()
let id = 5p3XT8TVFbVYNmFt0Kbu1ticaUT2

    let values = ["name": txf_Name.text ?? "", “email”: txf_email.text ?? "", “role”: txf_Role.text ?? ""]
    ref.child(id).updateChildValues(values, withCompletionBlock: { (error, ref) in
        if(error != nil){
            print(error?.localizedDescription ?? "")
            return
        }
        print("Values Updated")            

    })

}
Related