Patrick Vibild
Patrick Vibild

Reputation: 360

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.... ; 
Task t2 = Query2.get().onSuccesListener.... ; 
Task t3 = Query3.get().onSuccesListener.... ; 
Task t4 = Query4.get().onSuccesListener.... ; 

I couldnt find anyway to do them with Task in Android so I tried to run a query inside of a AsynTaskand then wait for all AsyncTask to be done by using a Latch onPostExecuted.

Here an example of one of my AsyncTask with the query.

private class GetLandlordsRoomQuery extends AsyncTask<Void, Void, Void> {
        ArrayList<RoomPosted> landlordsRooms = new ArrayList<>();
        @Override
        protected Void doInBackground(Void... voids) {
            Query removeNonTenant = FirebaseFirestore.getInstance()
                    .collection(DataBasePath.ROOMS.getValue())
                    .whereEqualTo("landlordID", FirebaseAuth.getInstance().getUid());

            removeNonTenant.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (DocumentSnapshot d : queryDocumentSnapshots) {
                        landlordsRooms.add(d.toObject(RoomPosted.class));
                    }
                }
            });
            return null;
        }
        @Override
        public void onPostExecute(Void result) {
            mLandlordsRooms = landlordsRooms;
            if (--asyncTaskLatch == 0){
                startFragmentFromLandlord();
            }
        }
    }

The problem that I run in here is that the onPostExecuted runs before the query is completed.

I might have the wrong approach to solve this issue with AsyncTask but couldnt find a way using Task to wait for all 4 task to be completed without making a nested onCompleteListener with them.

Any ideas how I could run in paralell all 4 threads and wait for them to be completed before I load my frament?

Upvotes: 3

Views: 3721

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

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:

Task t1 = Query1.get();
Task t2 = Query2.get();
Task t3 = Query3.get();
Task t4 = Query4.get();

Task combinedTask = Tasks.whenAllSuccess(t1, t2, t3, t4).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 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.

Upvotes: 8

Related Questions