如何将用户UID存储在Firebase数据库中并将其映射到POJO [英] How to store a users UID in a firebase database and have it map to a POJO

查看:113
本文介绍了如何将用户UID存储在Firebase数据库中并将其映射到POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CONTEXT



我目前正在开发一个Firebase应用程序(在Android中)。我已经建立了认证,存储和数据库,没有问题可言。由于Firebase身份验证用户只有一些属性,因此我创建了一个用户POJO来存储未包含在Firebase身份验证用户内的其他用户信息(即名字,出生日期,国籍等) 。这个结构如下图所示:



图像中的UUID是用户各自的认证uid。这是因为:
$ b $ pre $ ref.child(users).child(auth.getUid())。setValue(user) ;

这个方法似乎与文档一致,因为它允许我限制写/读给账户所有者 auth.uid === $ uid 规则。此外,树中的所有属性映射到我的POJO如预期。

问题



我的一个最大的问题是我想将用户uid存储在POJO中。由于UID目前是作为父对象构造的,我不知道如何将其映射到用户POJO。我当然可以在最底层的树层中存储一个额外的字段。但是,这似乎是毫无意义的数据冗余。

问题

映射uid的最佳方式是一个相应的用户POJO类。理想情况下,我可以抓取10个用户来显示,并且当用户点击时,应用程序会加载配置文件活动。这就意味着要传递用户的uid,所以我可以这样说:

$ $ $ $ $ c $ ref.child(users).child(targetUID)

并获取点击的用户帐户。这意味着有他们的用户ID。

解决方案所以我复制了代码并添加了以下方法,因为适配器存储了快照。

  public String getItemKey(int position ){
返回mSnapshots.getItem(position).getKey();





$ b现在你可以使用setter来设置用户的UID。



编辑:
我正在考虑这方面的内容

User.java

  public class User {

private String name;
私人字符串电子邮件;
私人字符串UID;
$ b public User(){
}

public String getUID(){
return UID;

$ b $ public void setUID(String UID){
this.UID = UID;
}

public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;






在您的Firebase适配器的 populateView()

 字符串UID; 

FirebaseListAdapter< User> searchAdapter = new FirebaseListAdapter< User>(getActivity(),User.class,R.layout.user_layout,mRef){
@Override
protected void populateView(View v,User model,int position){ b $ b //如果你确实需要在这里设置UID
// model.setUID = getRef(position).getKey();
//否则,我只是设置一个字符串字段,如图所示
//并将其传递给在配置文件中获取UID
//的活动
UID = getRef (position).getKey
((TextView)v.findViewById(R.id.text1))。setText(model.getName());
((TextView)v.findViewById(R.id.text2))。setText(model.getEmail());
v.setOnClickListener(MainActivity.this);
}
};
@Override
public void onClick(View v){
Intent intent = new Intent(this,ProfileActivity.class);
intent.putStringExtra(UID_EXTRA,UID);
startActivity(intent);
}


CONTEXT

I am currently working on a Firebase application (in Android). I have set up authentication, storage and the database with no issues to speak of. As the Firebase auth user only has a few properties, I have created a "user" POJO to store additional user information that is not contained inside of the Firebase auth user (ie first name, date of birth, nationality, etc...). The structure of this is shown in the image below:

The UUID in the image is the users respective auth uid. This was achieved as such:

ref.child(users).child(auth.getUid()).setValue(user);

This approach seems in line with the documentation as it allows me restrict write/read to the account owner using the auth.uid === $uid rule. Furthermore, all of the properties inside of the tree map to my POJO as expected.

PROBLEM

My one big problem is that I want to store the users uid inside the POJO. As the UID is currently structured as the objects parent, I am not sure how to map it to the user POJO. I could of course store an additional field in the bottom tree level. However, that seems like pointless data redundancy.

QUESTION

What is the best way to map the uid to a respective "user" POJO class. Ideally I'd be able to fetch 10 users to display, and when one is tapped have the app load a profile activity. This would mean passing the uid of the user, so I can say:

ref.child(users).child(targetUID)

and fetch the tapped user account. This means having their uid.

解决方案

I had the same problem while using FirebaseRecyclerAdapter, so I copied the code and added the following method since the adapter stores the snapshots.

public String getItemKey(int position) {
    return mSnapshots.getItem(position).getKey();
}

Now you can just set the user's UID using a setter.

EDIT : I was thinking something along these lines

User.java

public class User {

    private String name;
    private String email;
    private String UID;

    public User() {
    }

    public String getUID() {
    return UID;
    }

    public void setUID(String UID) {
    this.UID = UID;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
}

And in your Firebase adapter's populateView()

String UID;

FirebaseListAdapter<User> searchAdapter = new FirebaseListAdapter<User>(getActivity(), User.class, R.layout.user_layout, mRef) {
        @Override
        protected void populateView(View v, User model, int position) {
            // If you really need to set the UID here              
            // model.setUID = getRef(position).getKey();
            // Otherwise, I would just set a String field as shown 
            //and pass it with the intent to get the UID 
            //in the profile Activity
            UID = getRef(position).getKey
            ((TextView) v.findViewById(R.id.text1)).setText(model.getName());
            ((TextView) v.findViewById(R.id.text2)).setText(model.getEmail());
                v.setOnClickListener(MainActivity.this);
        }
    };
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.putStringExtra(UID_EXTRA, UID);
        startActivity(intent);
    }

这篇关于如何将用户UID存储在Firebase数据库中并将其映射到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆