如何通过方法返回DocumentSnapShot?

时间:2018-01-29 10:42:52

标签: java android firebase google-cloud-firestore

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

2 个答案:

答案 0 :(得分:13)

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

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

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

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

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

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

答案 1 :(得分:0)

使用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/Fragment观察LiveData,然后在onChanged内进行所需的操作。

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