When using push()
Firebase creates a unique ID or key such as -KKm9iRSax-scGn7m3Lb
.
According to the docs:
The push() method generates a unique ID every time a new child is added to the specified Firebase reference.
And:
The unique ID generated by push() is based on a timestamp, so list items are automatically ordered chronologically.
Is this ID or key unique to the whole database or only to the reference (like /users
)?
Firebase uses a very popular style of NoSQL database: Key-value stores. The concept is not so complicated once you know the basic JSON syntax.
Is Firebase id unique? Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).
Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.
You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.
Firebase keys are part timestamp and part random characters. So while they should be unique across the whole database there is a small chance that two keys could end up being the same.
Firebase blog: The 2^120 Ways to Ensure Unique Identifiers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With