Firestore - Merging two queries locally
To merge 2 separate queries locally, I recommend you to use Tasks.whenAllSuccess()
method. You can achieve this, using the following lines of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query firstQuery = rootRef...
Query secondQuery = rootRef...
Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();
Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
}
});
As you can see, when overriding the onSuccess()
method the result is a list
of objects which has the exact order of the tasks that were passed as arguments into the whenAllSuccess()
method.
There is also another approach and that would be to use Tasks.continueWith()
method. But according to the use-case of your app, you can use eiter whenAllSuccess()
method or continueWith()
method. Please see here the official documentation.