Search code examples
javaandroidfirebasefirebase-realtime-databasegeofire

Make identical node in firebase realtime database


Suppose there are two nodes:

  1. user data (inside "user data" we have:)

    unique id name: "abc" latitude: "123" longitude: "456"

  2. user key (inside "user key" we have:)

    unique id g: "awe46q" l 0:123 1:456

Here I want to save both "user data" & "user key" with the same unique id. But I don't know how to do it.


Solution

  • I have tried the push() method but it generates different unique ids.

    That's expected behavior since calling push() multiple times, will always generate each time a new random id.

    Will I have to use FirebaseAuth to make the unique id similar for "user data" & "user key".

    When it comes to users IDs, yes, the most appropriate solution is to implement Firebase Authentication and use the UID that is generated in this process. In the end, you can simply use the same UID, in every reference that you need, for example:

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference userDataRef = rootRef.child("userData").child(uid); //One reference
    userDataRef.setvalue(yourObject);
    DatabaseReference otherUserDataRef = rootRef.child("otherUserData").child(uid); //Second reference
    otherUserDataRef.setvalue(otherObject);