Ninja
Ninja

Reputation: 358

updateChildValues seem not only updates value

I am novice Firebase framework, so in tutorial i was trying to accomplish, author creates new value by just calling updateChildValues func only.

 @objc func handleRegister () {
    guard let email = emailTextField.text, let password = passwordTextField.text else {
        print("Form is not valid")
        return
    }
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error != nil {
            print(error)
            return
        }

        //successfully authenticated user
        let ref = Database.database().reference(fromURL: "https://my-awesome-project-18f30.firebaseio.com")
        ref.updateChildValues([ "someValue" : 123123])

    }

So it seems that updateChildValues func not only updates old value, but (in addition) creates new value if it did not exist before. I tried to search any mention in the web but fount only info regrading update old value and no info about creating new one if old did not exist before. Are my thoughts about updateChildValues func work true?

Upvotes: 1

Views: 153

Answers (1)

Pablo Marrufo
Pablo Marrufo

Reputation: 885

Yes the updateChildValues is like a PUT in an HTTP method. If the value for the key exists, it is updated. Else if the the value doesn't exists, the value is created.

As Frank van Puffelen, says

Firebase automatically creates keys for each value, and automatically removes keys without a value. Knowing this you can indeed see that updateChildValue can also be used to create values, and to remove keys by updating them with a null value.

Upvotes: 3

Related Questions