简体   繁体   English

从Firebase Messenger应用程序下载用户

[英]Download user from Firebase, Messenger app

I'm trying to write messenger app using Firebase. 我正在尝试使用Firebase编写Messenger应用。

In database I have a few entries, which are User.class objects. 在数据库中,我有一些条目,它们是User.class对象。 I'm trying to write function which can download User object from database. 我正在尝试编写可以从数据库下载User对象的函数。 I though that it'd be better to build separate class (UserManager) for this task, because I don't like making mess in code. 我虽然最好为此任务建立单独的类(UserManager),因为我不喜欢使代码混乱。 But there is a problem, because in onCreate method I need to use User object to download some additional info from database to create conversation list, so downloading user from server should be done before that. 但是有一个问题,因为在onCreate方法中,我需要使用User对象从数据库下载一些其他信息以创建对话列表,因此应该在此之前从服务器下载用户。 Also if user is not in database, it should create and push User to database using FirebaseAuth (I've got that working). 另外,如果用户不在数据库中,它应该使用FirebaseAuth创建用户并将其推送到数据库(我已经完成了工作)。

Should I build class extending AsynchTask, and there put downloading user, and then updating UI with the data downloaded after user ? 我应该建立扩展AsynchTask的类,然后放置下载用户,然后用用户之后下载的数据更新UI吗? How do I know if the user was already downloaded. 我如何知道用户是否已经下载。 Probably I should build some listener but I don't know how to do that. 也许我应该建立一些监听器,但是我不知道该怎么做。

Additional question: 附加问题:

If I use this reference with value listener, do i get a user object or some value from inside of the object? 如果我将此引用与值侦听器一起使用,是否从对象内部获取用户对象或某个值?

DatabaseReference userReference = FirebaseDatabase.getInstance().getReference().child("users/" + mUserID);

Here is my database: 这是我的数据库:

在此处输入图片说明

Each entry key is userID from FirebaseAuth for easier implementation. 每个条目密钥都是FirebaseAuth中的userID,以便于实现。

I've been cracking my head on this for a few days and tried different approaches. 我已经为此努力了几天,并尝试了不同的方法。 I'll apriciate any help. 我会尽力的。 I think, that some code or a scheme would be a huge help. 我认为,某些代码或方案会很有帮助。

Maybe this part of my code will help you figure out: 也许我的代码的这一部分将帮助您找出:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users")
.child(mUserID);

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e(TAG, "DatabaseError: " + databaseError.getMessage());
    }
});

How do I know if the user was already downloaded? 我如何知道用户是否已经下载?

You can add a flag to each user with the value of false and once you have downloaded the user object, to set the value to true but this is not how things are working with Firebase. 您可以为每个用户添加一个值为false的标志,并在下载用户对象后将其值设置为true但这不是Firebase的工作方式。 You cannot know when a user from the database is completed downloaded becase Firebase is a realtime database and getting data might never complete. 如果Firebase is a realtime database并且您可能永远都无法完成获取数据,则无法确定何时从数据库中下载用户。 That's why is named a realtime database because in any momemnt the data under that User object can be changed, properties can be added or deleted. 这就是为什么将其命名为实时数据库的原因,因为在任何菜单项中都可以更改该User对象下的数据,可以添加或删除属性。

You can use a CompletionListener only when you write or update data and you'll be notified when the operation has been acknowledged by the Database servers but you cannot use this interface when reading data. 您只能在写入更新数据时使用CompletionListener ,并且在数据库服务器确认该操作后,您会收到通知,但在读取数据时不能使用此接口。

If I use this reference with value listener, do i get a user object or some value from inside of the object? 如果我将此引用与值侦听器一起使用,是否从对象内部获取用户对象或某个值?

If the value that you are listening to is a User object, then you'll get a User object. 如果您正在侦听的值是一个User对象,那么您将获得一个User对象。 If the value is another type of object, which can also be a String (which is also an object) then you'll get that type of object, which can also be a String object. 如果该值是另一种对象,也可以是String (也是对象),那么您将获得该类型的对象,也可以是String对象。 Remember, that only the keys in a Firebase database are always strings. 请记住,只有Firebase数据库中的键始终是字符串。

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

相关问题 有没有办法跟踪或监听用户从浏览器、任何聊天应用程序甚至短信中点击任何链接? - Is there any way to trace or listen that user clicks on any link from a browser, or any chat messenger app or even SMS? Firebase身份验证无法从Playstore下载应用。 - Firebase authentication failing in app download from playstore. 如何通过Messenger发送来自应用的图片? - How to send image from app via messenger? 从 Uid 获取用户电子邮件,无需登录应用 Firebase - Get User Email From Uid Without Login to app Firebase 从Firebase存储下载音频 - Download audio from Firebase storage 从 firebase 存储下载文件并在需要时显示它,即使应用程序处于离线状态 - Download files from firebase storage and display it whenever required even when app is offline Firebase:获取 Firebase 当前用户使应用程序崩溃 - Firebase: Getting Firebase Current User Crashes The App 如何从 firebase 身份验证中删除用户,以便用户自动从应用程序中注销? - How to delete the user from firebase auth so the user will automatically logged out from the app? 通过URL从Firebase存储下载视频 - Download video from Firebase storage by URL 无法从Firebase存储下载图像 - Unable to download Images from Firebase Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM