如何返回 DocumentSnapShot 作为方法的结果?

一个自定义对象,其参数为 (DocumentSnapShot documentsnapShot)。也是Firebase中的一个内部对象,它检索快照并将值设置为我的自定义模型,也具有其参数(DocumentSnapShot 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<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"));
        }
    });
}

答案 1

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

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

还有一件事要提一下,您不需要将这些值设置为已经具有这些值的对象。您已经从数据库中获取了作为对象的数据。

因此,请记住,方法具有异步行为,这意味着甚至在从数据库获取数据之前也会调用该行为。如果要在该方法之外使用对象,则需要创建自己的 .要实现此目的,首先您需要创建一个如下界面:onSuccess()settingscallback

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

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

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

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

这是使用该对象的类外部方法的唯一方法。有关更多信息,您还可以观看此视频UserAccountSettingsonSuccess()


答案 2

用作返回类型并观察其值的变化以执行所需的操作。LiveData

private MutableLiveData<UserAccountSettings> userSettingsMutableLiveData = new MutableLiveData<>();

public MutableLiveData<UserAccountSettings> getUserSettings(DocumentSnapshot documentSnapshot){

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

            userSettingsMutableLiveData.setValue(settings);
        }
    });

    return userSettingsMutableLiveData;
}

然后从你的观察和内部做你想要的操作。Activity/FragmentLiveDataonChanged

getUserSettings().observe(this, new Observer<UserAccountSettings>() {
    @Override
    public void onChanged(UserAccountSettings userAccountSettings) {
        //here, do whatever you want on `userAccountSettings`
    }
});

推荐