Firestore Android - Correct way to merge 2 queries

1,643 views
Skip to first unread message

flo.wa...@gmail.com

unread,
May 2, 2018, 10:25:48 AM5/2/18
to Firebase Google Group
Hello,
I want to run 2 queries to effectively create and != operator. Each query uses the orderBy method to order the results. But when I run these 2 queries independently, it's not guaranteed that one finishes before the other, right? 
I am trying to figure out how to merge these 2 tasks locally to get the results in the correct order. There is no specific documentation about this case in Firestore. There is general Firebase documentation, but I don't know if I put 2 and 2 together properly, because there are multiple way of doing this I can think of.

Should I merge these 2 queries with whenAllSuccess? Is this the appropriate way of doing this? If yes, how do I use the List<Object> objects? It contains my QuerySnapshots, right? But how do I cast it properly? I am confused.

(I just put them into a TextView for testing purposes. I know that I should use a RecyclerView instead)

Enter code here...public void loadNotes(View v) {
    Task task1 = notebookRef
.whereLessThan("priority", 2)
.orderBy("priority")
.get();

Task task2 = notebookRef
.whereGreaterThan("priority", 2)
.orderBy("priority")
.get();

Tasks.whenAllSuccess(task1, task2).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> objects) {
String data = "";
for (Object object : objects) {
QuerySnapshot queryDocumentSnapshots = (QuerySnapshot) object;
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Note note = documentSnapshot.toObject(Note.class);
note.setDocumentId(documentSnapshot.getId());

String documentId = note.getDocumentId();
String title = note.getTitle();
String description = note.getDescription();
int priority = note.getPriority();

data += "ID: " + documentId
+ "\nTitle: " + title + "\nDescription: " + description
+ "\nPriority: " + priority + "\n\n";
}

textViewData.setText(data);
}
}
});
}


Samuel Stern

unread,
May 2, 2018, 11:58:48 AM5/2/18
to fireba...@googlegroups.com
Hi there,

Your basic idea is 100% correct, you can do each query separately and uses Tasks.whenAllSuccess() to know when they are both done.  The List<Object> you get is the list of task results in the order that you passed the tasks in, so your assumption there is correct too.  However since you still have references to task1 and task2 hanging around, you could skip the ugly cast and do something like this instead:

final Task<QuerySnapshot> task1 = notebookRef

.whereLessThan("priority", 2)
.orderBy("priority")
.get();

final Task<QuerySnapshot> task2 = notebookRef

.whereGreaterThan("priority", 2)
.orderBy("priority")
.get();

Tasks.whenAllSuccess(task1, task2).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> objects) {
        // Ignore the List<Object> since we still have references to task1 and task2!
QuerySnapshot results1 = task1.getResult();
QuerySnapshot results2 = task1.getResult();

// Continue with your logic from before
// ...
}
});

Hope that makes sense!

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/99ee4ec9-e179-47b1-988f-5ef55eebbb15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

flo.wa...@gmail.com

unread,
May 2, 2018, 5:00:38 PM5/2/18
to Firebase Google Group
Thank you for your answer!
The cast is indeed ugly. My approach was to save the return value of whenAllSuccess into a Task<List<QuerySnapshot>>. This way the OnSuccessListener doesn't return a List<Object> but List<QuerySnapshot>. What do you think? I am a beginner so I wonder if I should use generics here for the reason of code readability.

Samuel Stern

unread,
May 2, 2018, 5:32:30 PM5/2/18
to fireba...@googlegroups.com
That will work as well! 

- Sam

flo.wa...@gmail.com

unread,
May 3, 2018, 10:05:44 AM5/3/18
to Firebase Google Group
Thank you, that was very helpful!
Reply all
Reply to author
Forward
0 new messages