英文:
Make identical node in firebase realtime database
问题
假设有两个节点:
-
用户数据(在“用户数据”内部包含):
唯一ID
姓名:“abc”
纬度:“123”
经度:“456” -
用户密钥(在“用户密钥”内部包含):
唯一ID
g:“awe46q”
l
0:123
1:456
在这里,我想要使用相同的唯一ID保存“用户数据”和“用户密钥”,但我不知道如何做到这一点。
英文:
Suppose there are two nodes:
-
user data (inside "user data" we have:)
unique id
name: "abc"
latitude: "123"
longitude: "456" -
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.
答案1
得分: 0
> 我尝试过使用push()方法,但它生成了不同的唯一标识。
这是预期的行为,因为多次调用push()
将每次生成一个新的随机标识符。
> 我是否需要使用Firebase身份验证来使“用户数据”和“用户密钥”的唯一标识类似。
当涉及到用户ID时,是的,最合适的解决方案是实施Firebase身份验证并使用在此过程中生成的UID
。最后,您可以在需要的每个引用中简单地使用相同的UID
,例如:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference userDataRef = rootRef.child("userData").child(uid); //一个引用
userDataRef.setvalue(yourObject);
DatabaseReference otherUserDataRef = rootRef.child("otherUserData").child(uid); //第二个引用
otherUserDataRef.setvalue(otherObject);
英文:
> 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论