如何从DocumentSnapShot返回keySet?

时间:2019-01-31 06:23:11

标签: java android firebase mvvm google-cloud-firestore

我一直在努力我公司的FireStore数据库和我的应用程序之间实现了一个抽象层,但我无法弄清楚如何收集数据,我需要。

我正在尝试检索文档的字段并将所有密钥存储在Set<String>中。我知道我可以读取正确的密钥,因为存储库中的日志会写入它们,但是我似乎一直在返回null内的ViewModel并因此返回Fragment

下面是我用来检索密钥的Repository方法:

public Set<String> getGroups(){
    Log.i(TAG,"Attempting to retrieve a user's groups.");

    DocumentReference mDoc;
    mDoc = userCollection.document(currentUser.getUid());
    mDoc.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot snapshot,
                            @Nullable FirebaseFirestoreException e) {
            if (e != null){
                Log.w(TAG,"Listen failed.", e);
            }
            if (snapshot != null && snapshot.exists() ){
                Log.d(TAG,"Current data: " + snapshot.getData());
                mySet = snapshot.getData().keySet();
            } else {
                Log.d(TAG,"Current data: null");
            }
        }
    });

    return mySet;
}

我的ViewModel调用存储库方法:

public class FirebaseUserViewModel extends ViewModel {
    private static final String TAG = "FirebaseUserViewModel";

    private FirebaseRepository mRepository;

    public FirebaseUserViewModel(){
        this.mRepository = new FirebaseRepository();
    }

    public Set<String> getGroups(){
        return mRepository.getGroups();
    }

我尝试收集密钥集的Fragment中的代码段:

    myKeySet = mData.getGroups();
    Log.d(TAG,"Current data via repository call: " + myKeySet);

摘录中的日志:

D/MyGroups_Fragment: Current data via repository call: null

作为旁注,通过将SnapShotListener添加到文档中,我的片段会知道文档中的更新吗?我最初的意图是返回LiveData<Set<String>>,但是几个小时后我没有任何进展,所以我想将问题分解为仅返回集合。

任何指导将不胜感激。谢谢。

编辑

根据Alex的建议,一旦从Firestore加载了数据,我便实现了一个接口来收集数据。但是,我仍然不确定如何将存储库中的加载数据访问ViewModel,最后访问Fragment。

private Set<String> mKeySet;

public Set<String> getGroups(){

    loadGroups(new FirestoreCallBack() {
        @Override
        public Set<String> onCallBack(Set<String> keySet) {
            return mKeySet = keySet;
        }
    });
    return mKeySet;
}

private void loadGroups(final FirestoreCallBack firestoreCallBack){
    Log.i(TAG,"Attempting to retrieve a user's groups.");
    userCollection.document(currentUser.getUid()).get().addOnCompleteListener(
            new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()){
                        DocumentSnapshot document = task.getResult();
                        Log.i(TAG,"Success inside the onComplete method of our document .get() and retrieved: "+ document.getData().keySet());
                        firestoreCallBack.onCallBack(document.getData().keySet());
                    } else {
                        Log.d(TAG,"The .get() failed for document: " + currentUser.getUid(), task.getException());
                    }
                }
            });
    Log.i(TAG, "Added onCompleteListener to our document.");
}

public interface FirestoreCallBack{
    Set<String> onCallBack(Set<String> keySet);
}

0 个答案:

没有答案