centenond
centenond

Reputation: 1198

Firebase admin, does push().getKey() guarantee me a unique key?

I want to use the key that is generated when push() so I can use the key in onSuccesListener, from what I understand this key is unique.

So instead of ref.push().setValue()

I did this ...

final String key = ref.push().getKey(); //unique guaranteed?

 ref.child(key).setValue(object).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                // so now I can use the key here
            }
        })

what its bothering me is this key is generated locally(I think, since I dont get it with a listener), and if is generated locally I dont know if it would be always unique?

If not, how can I approach this goal?

Upvotes: 0

Views: 582

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

Yes, calling push() generates an ID locally (which ensures can run while your user is not connected to the Firebase servers). And yes, they are statistically guaranteed to be unique. To learn more about Firebase's push ID, see this blog post: The 2^120 Ways to Ensure Unique Identifiers.

Also see:

Upvotes: 4

Related Questions