I'm assigning two Firestore Queries to LiveData objects. Each one of these queries returns to different object classes: User and Book. I need to merge both of them together and apply a transformation so I can create a LinkedHashmap that combines both sets of data (queries). In order to do this I'm using MediatorLive data and to be able to merge the two different classes I'm using QuerySnapshot as the return type.
The problem I'm running into is when I output the results of the MediatorLiveData object I get the results twice and compounded. Also, the first time results return null for the Book class and the second time results return null for the User class.
What am I missing?
Here's my code simplified:
These are my Firestore queries and LiveData assignments:
//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());
Below is my MediatorLiveData and associate get method:
//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();
}
And in MainActivity, here's my observer:
//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);
}
}
});
Finally, here is my output of the resulting merge:
MainActivity: OBSERVE MERGE users: [User{userID='OtlmIQxeLkvNYExsxjg4', userName='Lisa', lastDisplayedBookID='none', isAllBooks=false, dateCreated=Wed May 23 11:27:20 PDT 2018, dateModified=Wed May 23 11:27:20 PDT 2018, allBookID='akuGpq7aMB85J9QMtsBi', groupID='S0Ql3hnHmXACUqeGW04n'}]
MainActivity: OBSERVE MERGE books: [Book{bookAssigned=false, bookName='null', bookType='null', assignedTo='null', bookID='null', groupID='S0Ql3hnHmXACUqeGW04n', userID='OtlmIQxeLkvNYExsxjg4', dateCreated=Wed May 23 11:27:20 PDT 2018, lastAssignedDate=null, bookNum=0}]
MainActivity: OBSERVE MERGE users: [User{userID='OtlmIQxeLkvNYExsxjg4', userName='null', lastDisplayedBookID='null', isAllBooks=false, dateCreated=null, dateModified=null, allBookID='null', groupID='S0Ql3hnHmXACUqeGW04n'}, User{userID='OtlmIQxeLkvNYExsxjg4', userName='null', lastDisplayedBookID='null', isAllBooks=false, dateCreated=Wed May 23 11:27:20 PDT 2018, dateModified=null, allBookID='null', groupID='S0Ql3hnHmXACUqeGW04n'}]
MainActivity: OBSERVE MERGE books: [Book{bookAssigned=false, bookName='All Books', bookType='NA', assignedTo='Lisa', bookID='akuGpq7aMB85J9QMtsBi', groupID='S0Ql3hnHmXACUqeGW04n', userID='OtlmIQxeLkvNYExsxjg4', dateCreated=null, lastAssignedDate=null, bookNum=0}, Book{bookAssigned=true, bookName='Hunger Games', bookType='Soft Cover', assignedTo='Lisa', bookID='2D9C293DCB4469904532FF3FD81B3E7B', groupID='S0Ql3hnHmXACUqeGW04n', userID='OtlmIQxeLkvNYExsxjg4', dateCreated=Wed May 23 11:27:20 PDT 2018, lastAssignedDate=Wed May 23 11:27:20 PDT 2018, bookNum=1}]
As you can see the results seem to compound but with null values.
Any help on this would be greatly appreciated!