Skip to content Skip to sidebar Skip to footer

Run Multiple Firestore Queries And Wait For All Of Them To Be Completed

In my application I would like to perform 4 queries to my database before starting a fragment. Somehow what im trying to achieve is: Task t1 = Query1.get().onSuccesListener.... ;

Solution 1:

You can simply merge all your 4 separate queries locally, using Tasks's whenAllSuccess() method. You can achieve this, using the following lines of code:

Taskt1= Query1.get();
Taskt2= Query2.get();
Taskt3= Query3.get();
Taskt4= Query4.get();

TaskcombinedTask= Tasks.whenAllSuccess(t1, t2, t3, t4).addOnSuccessListener(newOnSuccessListener<List<Object>>() {
    @OverridepublicvoidonSuccess(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 that you get from those queries.

The Cloud Firestore client, already runs all network operations in a background thread. This means that all operations take place without blocking your main thread. Putting it in an AsyncTask does not give any additional benefits.

Post a Comment for "Run Multiple Firestore Queries And Wait For All Of Them To Be Completed"