Raim Khalil
Raim Khalil

Reputation: 387

Firebase Child By AutoID with multiple users?

Currently, I am using firebase for a messaging app. For the messages, I use a childByAutoID() to create the new message. If two people create a message at the same time, will the ID be created correctly?

 let messageConnection = Database.database().reference().child("messages").child(convoID!).childByAutoId()
    messageConnection.updateChildValues(["content" : messageText ?? "incomplete", "sender": userHash, "time": time], withCompletionBlock: { error, ref in

    if error != nil{
        print("ERROR")
    }
    else{
        let conversationRef = Database.database().reference().child("conversations").child(self.convoID!)
        conversationRef.updateChildValues(["last_message" : messageText!,"last_message_time":time])
        textField.text = ""
    }

})

Upvotes: 1

Views: 532

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

I'm not sure what "correctly" means to you here.

But when two app instances call childByAutoId() at the exact same time, they'll still get a unique ID. The IDs are statistically guaranteed to be unique, and include a sufficient number of random bits to ensure that.

To learn more about these push IDs, read The 2^120 Ways to Ensure Unique Identifiers.

Upvotes: 3

Related Questions