How chain queries in firestore and wait retrieve data

Arno :

I would like to chain query in firestore. I need some informations about one collections before get other information in an other collection.

I have already try to use Tasks.whenall()... but doesn't efficient. I try to use callBack too.

Here my first function :

public static void getAllFavoris(){
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    FirebaseFirestore.getInstance().collection("product").document("favoris").collection(uid).get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {//task is succesful
                        Log.e("TAG","task succes for fav ");
                        for (QueryDocumentSnapshot document : task.getResult()){//never enter in this loop
                            Log.e("TAG","Doc "+document);
                            Log.e("TAG", "Succes for get all favoris");
                            Log.e("TAG","data for favoris ::: "+document.getId());
                            MainActivity.favorisList.add(document.getId());
                        }
                    }
                    else {
                        Log.d("TAG", "Error getting documents: ", task.getException());
                    }
//call without data retrieve
                    Log.e("TAG","favoris ::: "+showListContentS(MainActivity.favorisList));
                    getProductByTagFound();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e("TAG","error get All favoris"+e);
                }
            });
}

And here the second query i need :

public static void getProductByTagFound(){
for(int i=0;i<MainActivity.allTags.size();i++){ //allTags is not empty and i need to finish this loop
    String tagId = MainActivity.allTags.get(i).toString();
    FirebaseFirestore.getInstance().collection("product").document("allProduct").collection("productByTag").document(tagId).get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()){
                            Log.e("TAG", "Succes for get productByTag");
                            Product pdt = task.getResult().toObject(Product.class);
                            MainActivity.productByTag.add(pdt);

                    }
                }
            });
}
//this must be call after the loop is finish but call in the same time.
Log.e("TAG","Get product BY Tag"+showListContentP(MainActivity.productByTag));
createFinalList();

}

I need that to call createFinalList() after the loop finish and also to enter in the loop for favoris get data and call getProductByTag() after.

Alex Mamo :

If you want to perform a new query right after the first one is finished, you need to wait untill the first query completes. To sovle this, you need to use nested queries. With other words, you need to move the second query right inside the first callback, inside onComplete() method. In this way, the second query will be performed only when the first one completes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155338&siteId=1