Nikola
Nikola

Reputation: 890

Firebase Realtime Database obtain push() reference

This is how I save my Events in my app:

FirebaseDatabase.getInstance().getReference().child("events").push().setValue(ev);

enter image description here

Now I would like each event to have its own chat room where users can send messages to each other. However, I need to know the reference to the event so I can add the chat room. I don't want to change the Event object to have this property. How do I do that?

databaseRef = FirebaseDatabase.getInstance().getReference().child("events").child(?);

Upvotes: 0

Views: 897

Answers (1)

M A
M A

Reputation: 72864

You can reference the pushed event using DatabaseReference#getKey(), so to retrieve the newly added event, call:

String eventId = FirebaseDatabase.getInstance().getReference().child("events").push()
                                                                              .getKey()

Then you can do:

FirebaseDatabase.getInstance().getReference().child("events").child(eventId)...

Upvotes: 1

Related Questions