1

I am trying to create a child in an empty firebase database using Swift, the code is running without any errors but I don't see the changes in the database. the code:

let ref = Database.database().reference()
ref.child("Branches")
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mor Goren
  • 105
  • 9

2 Answers2

6

You're not adding values to the database.

In Firebase, you create additional childs as soon as you set new values.

You can use setValue or updateChildValues to add values.

Example:

let ref = Database.database().reference().child("branches")

let values = ["any_key" : "any_value"]
ref.updateChildValues(values) { (err, reference) in
    // handle errors or anything related to completion block.
}

Docs: https://firebase.google.com/docs/database/ios/read-and-write


Note: Make sure you configured everything correctly: https://firebase.google.com/docs/database/ios/start

Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
  • Is it not possible to create a child without input? I can create one online on Firebase but not with coding? – submariner May 15 '22 at 14:46
1

It's depend on your database structure let suppose you create

{ 
      "users": {
        "uid": userID,
            "username": "string"
      }
}

this time data then you can get it as you mention in your question

var ref: DatabaseReference!
ref = Database.database().reference()
self.ref.child("users").child(user.uid).setValue(["username": username])
  • In this can be some mistake but you have to understand concept,
Naveen Yadav
  • 81
  • 1
  • 8