firebase

How to get push key value from Firebase Database?

Introduction#

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

Android Example

Let’s assume we have a Dogs application, then our model will be a Dog class.

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dogs");

This is how to send a Dog to the database, a new unique dog and set the dog with the key.

String key = reference.push().getKey();
Dog dog = new Dog("Spike");
dog.setKey(key);
reference.child(key).setValue(dog);

The reference.child(key).setValue(dog); is equivalent of reference.push().setValue(dog); And add the benefit of getting the key inside the Dog object.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow