简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM