Search code examples
iosfirebaseswift3firebase-realtime-databasefirebase-authentication

iOS Swift Create Child in Firebase Realtime Database Programatically


I have configured my app with Firebase. In my app I have user creation and login methods (using their email and password). I need to create new child in Firebase's Database when a new user is created. How can I do this?


Solution

  • After you do the FIRAuth.auth()?.createUser, you can check if the error is nil or not. If it is not, then you know the user has been created, and therefore can add your new child. You can do this by writing:

    let uid: String = (FIRAuth.auth()?.currentUser?.uid)!
    FIRDatabase.database().reference().child(uid).setValue(arrayOfData)
    

    Therefore, the new node that you create is the user's Firebase ID, and will definitely be unique.