如何作为方法的结果返回 DocumentSnapShot? [英] How to return a DocumentSnapShot as a result of a method?

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

问题描述

一个带有参数的自定义对象 (DocumentSnapShot documentsnapShot).也是来自 Firebase 的一个内部对象,它检索快照并将值设置为我的自定义模型也有它的参数(DocumentSnapShot 文档快照).但是,我希望从 Firebase 获取数据并将其传递给我的自定义参数,因为我的不仅需要 Firebase 还需要多个数据.并且不可能在没有覆盖的情况下迭代 Firestore.

代码如下:

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() {@覆盖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 以异步加载数据,因为这可能需要一些时间.根据您的连接速度和状态,在该数据可用之前可能需要几百毫秒到几秒.如果您想将 settings 对象传递给另一个方法,只需在 onSuccess() 方法中调用该方法并将该对象作为参数传递.所以一个快速的解决方法是:

@Overridepublic void onSuccess(DocumentSnapshot documentSnapshot) {UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);你的方法(设置);}

<块引用>

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

所以请记住,onSuccess() 方法具有异步行为,这意味着它甚至在您从数据库中获取数据之前就会被调用.如果要在该方法之外使用 settings 对象,则需要创建自己的 callback.要实现这一点,首先你需要创建一个这样的界面:

公共接口 MyCallback {void onCallback(UserAccountSettings 设置);}

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

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

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

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

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

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.

Here's the code:

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"));
        }
    });
}

解决方案

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.

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);

        }
    });
}

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());
    }
});

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天全站免登陆