访问作为对象存储在Firestore数据库中的数据 [英] Accessing data stored as Object in Firestore database

查看:96
本文介绍了访问作为对象存储在Firestore数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,出于学习目的,我开始测试Firebase新文档db Firestore,现在我被困在访问文档内部的值存储对象.

Recently I started testing Firebase new document db Firestore for learning purpose, I am stuck right now to access the values store inside the document as object.

我正在使用以下代码访问存储在文档中的对象Privacy,但是我不确定如何访问Key - Value?例如,我在对象中有3个子Key - Value对,如何分别访问和编辑它?

I am using the below code to access object Privacy stored in document, but I am not sure how to access the Key - Value? For Example I have 3 sub Key - Value pair in the object, how will I access and edit it individually?

DocumentReference docRef = FirebaseFirestore.getInstance().collection("Users").document("PQ8QUHno6QdPwM89DsVTItrHGWJ3");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData().get("privacy"));
                Object meta_object = task.getResult().getData().get("privacy");
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

感谢您的帮助.

推荐答案

您可以将文档中的privacy字段视为Map<String, Boolean>,因此可以将该字段的值转换为这样的变量:

The privacy field within your document can be considered a Map<String, Boolean>, so you could cast the value of this field into such a variable:

HashMap<String, Boolean> privacy = (HashMap<String, Boolean>) task.getResult().getData().get("privacy");

现在的主要问题是,您可能会看到未经检查的转换"编译器警告,因为转换了这样并不理想,因为您不能保证数据库结构在此字段中始终包含String : Boolean值.

Now the main problem with this is that you'll likely see an "unchecked cast" compiler warning because casting a Map like this is not ideal as you can't guarantee that the database structure will always contain String : Boolean values in this field.

在这种情况下,我建议使用自定义对象进行存储& 检索数据库中的对象,它将自动处理为您编组和投放:

In this case, I would suggest using custom objects to store & retrieve objects in your database, which will automatically deal with marshalling and casting for you:

DocumentReference docRef = FirebaseFirestore.getInstance().collection("Users").document("PQ8QUHno6QdPwM89DsVTItrHGWJ3");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                User user = task.getResult().toObject(User.class);
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

您的User类类似于:

public class User {
    private String username;
    private HashMap<String, Boolean> privacy;

    public User() {}

    public User(String username, HashMap<String, Boolean> privacy) {
        this.username = username;
        this.privacy = privacy;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public HashMap<String, Boolean> getPrivacy() {
        return username;
    }

    public void setPrivacy(HashMap<String, Boolean> privacy) {
        this.privacy = privacy;
    }
}

在此示例中,User user = task.getResult().toObject(User.class)调用会将整个文档编组为User对象的实例,然后您可以使用以下命令访问隐私地图:

In this example, the User user = task.getResult().toObject(User.class) call will marshall the entire document into an instance of your User object, and you can then access the privacy map with:

HashMap<String, Boolean> userPrivacy = user.getPrivacy();

文档中的每个字段都将与您的自定义对象中具有相同名称的字段匹配,因此您也可以以相同的方式添加settingsphoto_url字段.您只需要记住:

Each field in the document will be matched to a field with the same name within your custom object, so you could also add the settings or photo_url fields in the same fashion. You just need to remember:

每个自定义类都必须具有不带任何参数的公共构造函数.此外,该类必须为每个属性包含一个公共获取器.

Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property.

这篇关于访问作为对象存储在Firestore数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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