如何通过方法返回DocumentSnapShot? [英] How to return a DocumentSnapShot as a result of a method?

查看:81
本文介绍了如何通过方法返回DocumentSnapShot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有参数(DocumentSnapShotdocumentsnapShot)的自定义对象.这也是Firebase的内部对象,用于检索快照并将值设置为我的自定义模型,也有其参数(DocumentSnapShotdocumentsnapShot).但是,我希望从Firebase获取数据并将其传递给我的自定义参数,因为我的不仅要获取Firebase的多个数据.而且,没有覆盖就不可能迭代Firestore.

A custom object that takes a parameter of (DocumentSnapShot documentsnapShot). also is an inner object from Firebase that retrieves a snapshot and set the values to my custom model also have its argument (DocumentSnapShot documentsnapShot). However, I wish to get the data from Firebase and pass it to my custom argument because mine takes multiple data not only Firebase. And it's not possible to iterate Firestore without an override.

代码如下:

public UserSettings getUserSettings(DocumentSnapshot documentSnapshot){
    Log.d(TAG, "getUserSettings: retrieving user account settings from firestore");

    DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
    mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
            settings.setDisplay_name(documentSnapshot.getString("display_name"));
            settings.setUsername(documentSnapshot.getString("username"));
            settings.setWebsite(documentSnapshot.getString("website"));
            settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
            settings.setPosts(documentSnapshot.getLong("posts"));
            settings.setFollowers(documentSnapshot.getLong("followers"));
            settings.setFollowing(documentSnapshot.getLong("following"));
        }
    });
}

推荐答案

您现在无法返回尚未加载的内容. Firestore加载数据asynchronously,因为此操作可能需要一些时间.根据您的连接速度和状态,可能需要几百毫秒到几秒钟的时间才能获得该数据.如果要将settings对象传递给另一个方法,只需在onSuccess()方法内部调用该方法,然后将该对象作为参数传递.因此,一个快速的解决方法是:

You cannot return something now that hasn't been loaded yet. Firestore loads data asynchronously, since it may take some time for this. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before that data is available. If you want to pass settings object to another method, just call that method inside onSuccess() method and pass that object as an argument. So a quick fix would be this:

@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
    UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
    yourMethod(settings);
}

还有一件事要提到,您不需要将那些值设置为已具有它们的对象.您已经作为对象从数据库中获取数据.

One more thing to mention is that you don't need to set the those values to object that already have them. You are already getting the data from the database as an object.

因此请记住,onSuccess()方法具有异步行为,这意味着甚至在从数据库中获取数据之前就已调用该方法.如果要在该方法之外使用settings对象,则需要创建自己的callback.为此,首先需要创建一个像这样的接口:

So remember, onSuccess() method has an asynchronous behaviour, which means that is called even before you are getting the data from your database. If you want to use the settings object outside that method, you need to create your own callback. To achieve this, first you need to create an interface like this:

public interface MyCallback {
    void onCallback(UserAccountSettings settings);
}

然后,您需要创建一个实际上从数据库中获取数据的方法.此方法应如下所示:

Then you need to create a method that is actually getting the data from the database. This method should look like this:

public void readData(MyCallback myCallback) {
    DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
    mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
            myCallback.onCallback(settings);

        }
    });
}

最后,只需调用readData()方法,并在需要的地方将MyCallback接口的实例作为参数传递,如下所示:

In the end just simply call readData() method and pass an instance of the MyCallback interface as an argument wherever you need it like this:

readData(new MyCallback() {
    @Override
    public void onCallback(UserAccountSettings settings) {
        Log.d("TAG", settings.getDisplay_name());
    }
});

这是在onSuccess()方法之外可以使用UserAccountSettings类的对象的唯一方法.有关更多信息,您还可以查看此 视频 .

This is the only way in which you can use that object of UserAccountSettings class outside onSuccess() method. For more informations, you can take also a look at this video.

这篇关于如何通过方法返回DocumentSnapShot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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