使用MediatorLiveData合并到LiveData(Firestore)QuerySnapshot流产生了奇怪的结果

时间:2018-05-23 18:49:30

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

我将两个Firestore查询分配给LiveData对象。这些查询中的每一个都返回到不同的对象类:User和Book。我需要将它们合并在一起并应用转换,这样我就可以创建一个组合了两组数据(查询)的LinkedHashmap。为了做到这一点,我使用了MediatorLive数据,并且能够使用QuerySnapshot作为返回类型合并我使用的两个不同的类。

我遇到的问题是当我输出MediatorLiveData对象的结果时,我得到结果两次并且复合。此外,第一次结果为Book类返回null,第二次结果为User类返回null。

我错过了什么?

这里简化了我的代码:

这些是我的Firestore查询和LiveData分配:

    //getUsers query using FirebaseQueryLiveData class
    private Query getUsersQuery() {
        FirebaseAuth mAuth = FirebaseAuth.getInstance();

        adminID = mAuth.getUid();
        query = FirebaseFirestore.getInstance().collection("admins")
                .document(adminID)
                .collection("users")
        return query;
    }

    private FirebaseQueryLiveData usersLiveData = new FirebaseQueryLiveData(getUsersQuery());


    //getBooks query using FirebaseQueryLiveData class
    private Query getBooksQuery () {
        FirebaseGroupID firebaseGroupID = new FirebaseGroupID(getApplication());
        groupID = firebaseGroupID.getGroupID();
        query = FirebaseFirestore.getInstance().collection("books")
                .whereEqualTo("groupID", groupID)      
        return query;
    }

    private FirebaseQueryLiveData booksLiveData = new FirebaseQueryLiveData(getBooksQuery()); 

下面是我的MediatorLiveData和关联get方法:

    //MediatorLiveData merge two LiveData QuerySnapshot streams
    private MediatorLiveData<QuerySnapshot> usersBooksLiveDataMerger() {
        final MediatorLiveData<QuerySnapshot> mediatorLiveData = new MediatorLiveData<>();
        mediatorLiveData.addSource(usersLiveData, new Observer<QuerySnapshot>() {
            @Override
            public void onChanged(@Nullable QuerySnapshot querySnapshot) {
                mediatorLiveData.setValue(querySnapshot);
            }
        });
        mediatorLiveData.addSource(booksLiveData, new Observer<QuerySnapshot>() {
            @Override
            public void onChanged(@Nullable QuerySnapshot querySnapshot) {
                mediatorLiveData.setValue(querySnapshot);
            }
        });
        return mediatorLiveData;
    }

    //get method to get our merged LiveData streams
    public MediatorLiveData<QuerySnapshot> getUsersBooksLiveDataMerger() {
        return usersBooksLiveDataMerger();
    } 

在MainActivity中,这是我的观察者:

    //Main Activity observer
    mainViewModel.getUsersBooksLiveDataMerger().observe(this, new Observer<QuerySnapshot>() {
            @Override
            public void onChanged(@Nullable QuerySnapshot querySnapshot) {
                if (querySnapshot != null) {
                    List<Book> books;
                    List<User> users;
                    books = querySnapshot.toObjects(Book.class);
                    users = querySnapshot.toObjects(User.class);
                    Log.d(TAG, "OBSERVE MERGE users: " + users);
                    Log.d(TAG, "OBSERVE MERGE books: " + books);
                }
            }
        }); 

最后,这是我得到的合并的输出:

  

MainActivity:观察MERGE用户:[用户{用户ID =&#39; OtlmIQxeLkvNYExsxjg4&#39 ;,的userName =&#39;莉莎&#39 ;, lastDisplayedBookID =&#39;无&#39 ;, isAllBooks =假,dateCreated会=星期三5月23日11时27分二十秒PDT 2018,dateModified =星期三5月23日11时27分二十秒PDT 2018,allBookID =&#39; akuGpq7aMB85J9QMtsBi&#39 ;,组ID =&#39; S0Ql3hnHmXACUqeGW04n&#39;}]

     

MainActivity:OBSERVE MERGE书籍:[预订{bookAssigned = false,bookName =&#39; null&#39;,bookType =&#39; null&#39;,assignedTo =&#39; null&#39;,bookID =&#39;空&#39 ;,组ID =&#39; S0Ql3hnHmXACUqeGW04n&#39 ;,用户ID =&#39; OtlmIQxeLkvNYExsxjg4&#39 ;, dateCreated会=星期三5月23日11时27分20秒PDT 2018,lastAssignedDate = NULL,bookNum = 0}]

     

MainActivity:观察MERGE用户:[用户{用户ID =&#39; OtlmIQxeLkvNYExsxjg4&#39 ;,的userName =&#39;空&#39 ;, lastDisplayedBookID =&#39;空&#39 ;, isAllBooks =假,dateCreated会= NULL,dateModified = NULL,allBookID =&#39;空&#39 ;,组ID =&#39; S0Ql3hnHmXACUqeGW04n&#39;},用户{用户ID =&#39; OtlmIQxeLkvNYExsxjg4&#39 ;,的userName =&#39;空& #39;,lastDisplayedBookID =&#39; null&#39;,isAllBooks = false,dateCreated = Wed May 23 11:27:20 PDT 2018,dateModified = null,allBookID =&#39; null&#39;,groupID =& #39; S0Ql3hnHmXACUqeGW04n&#39;}]

     

MainActivity:OBSERVE MERGE书籍:[预订{bookAssigned = false,bookName =&#39;所有图书&#39;,bookType =&#39; NA&#39;,assignedTo =&#39; Lisa&#39;, bookID =&#39; akuGpq7aMB85J9QMtsBi&#39;,groupID =&#39; S0Ql3hnHmXACUqeGW04n&#39;,userID =&#39; OtlmIQxeLkvNYExsxjg4&#39;,dateCreated = null,lastAssignedDate = null,bookNum = 0},预订{bookAssigned =是的,bookName =&#39;饥饿游戏&#39;,bookType =&#39; Soft Cover&#39;,assignedTo =&#39; Lisa&#39;,bookID =&#39; 2D9C293DCB4469904532FF3FD81B3E7B&#39;,groupID = &#39; S0Ql3hnHmXACUqeGW04n&#39 ;,用户ID =&#39; OtlmIQxeLkvNYExsxjg4&#39 ;, dateCreated会=星期三5月23日11点27分20秒PDT 2018,lastAssignedDate =星期三5月23日11点27分20秒PDT 2018,bookNum = 1} ]

正如您所看到的,结果似乎复合但具有空值。

对此的任何帮助将不胜感激!

0 个答案:

没有答案