简体   繁体   English

Firebase / Android - 由UID加载用户

[英]Firebase/Android - Load User by UID

I have only found code snippets to load the currently logged in user: 我只找到了加载当前登录用户的代码片段:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

But what if I need to load another user's data, like the image path and name? 但是,如果我需要加载其他用户的数据,如图像路径和名称,该怎么办? I want to load them by their UID. 我想用他们的UID加载它们。

I could of course save the data myself like this: 我当然可以像这样保存数据:

-> users
    -> UID
        -> imagePath
        -> name

But I thought there might be a way to use the FirebaserUser class. 但我认为可能有一种方法可以使用FirebaserUser类。

I actually think there has to be some way, it's just not written in the docs. 我实际上认为必须有某种方式, 它只是没有写在文档中。

There's no way to do this directly using FirebaseUser class. 使用FirebaseUser类无法直接执行此操作。 The old docs of Firebase 1.0 (Before it was acquired by Google) mentioned that exact scenario and the possible solution. Firebase 1.0的旧文档(在被Google收购之前)提到了确切的场景和可能的解决方案。 The current doc is not completed yet. 目前的文档尚未完成。

The solution was to save the user's data in the Real-time database keeping the uid as the key. 解决方案是将用户的数据保存在Real-time database并将uid作为密钥。 So when you need the data of another user, you'll just have to remember their UID and can search for their data (Exactly like how you mentioned in the question description). 因此,当您需要其他用户的数据时,您只需要记住他们的UID并可以搜索他们的数据(完全像您在问题描述中提到的那样)。

I searched the other way to do that without using Real-time database when I started working on my own app, however after finding no other method and reading the old doc, I gave up and used the database way. 当我开始使用我自己的应用程序时,我在不使用实时数据库的情况下搜索了另一种方法,但是在找不到其他方法并阅读旧文档之后,我放弃并使用了数据库方式。 Trust me it's way easier and flexible to use the database, instead of hacking your way to avoid using it altogether. 相信我使用数据库更容易,更灵活,而不是黑客入侵,以避免完全使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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