Search code examples
androidfirebase-realtime-databasembaas

How DatabaseReference#push() method differs from location to location?


When using Firebase Database I can generate a key using push() method of a DatabaseReference object as shown in the code snippets below:

FirebaseDatabase.getInstance().getReference("/location").push().getKey();

or

FirebaseDatabase.getInstance().getReference().push().getKey();

Is there a difference in the above usage?

Please note that I am concerned about the key only, not the returned DatabaseReference from push

Does push method introduce side effects on my online Firebase Database?


Solution

  • The push() method is a pure client-side operation that generates a key that is both chronologically, lexicographically ordered and statistically guaranteed to be unique.

    There is (currently) no difference between calling push() on one location or another. The method to generate the keys is the exact same and does not depend on the location.

    For more on Firebase push ID, I recommend reading the blog post The 2^120 Ways to Ensure Unique Identifiers.