Java firestore和wait中的链查询如何检索数据

Java firestore和wait中的链查询如何检索数据,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我想在firestore中链接查询。我需要一些关于一个集合的信息,然后才能在另一个集合中获得其他信息 我已经尝试使用任务。whenall()。。。但是没有效率。 我也尝试使用回调 这里是我的第一个功能: public static void getAllFavoris(){ String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); FirebaseFirestore.getInstance().colle

我想在
firestore
中链接查询。我需要一些关于一个集合的信息,然后才能在另一个集合中获得其他信息

我已经尝试使用
任务。whenall()
。。。但是没有效率。 我也尝试使用
回调

这里是我的第一个功能:

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);
                }
            });
}
publicstaticvoid getallfavoritis(){
字符串uid=FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore.getInstance().collection(“产品”).document(“favoris”).collection(uid.get())
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
如果(task.issusccessful()){//任务成功
Log.e(“标签”,“fav任务成功”);
对于(QueryDocumentSnapshot文档:task.getResult()){//从不在此循环中输入
Log.e(“标签”、“文件”+文件);
Log.e(“标记”,“获得所有优惠的成功”);
Log.e(“TAG”,“favoris的数据::”+document.getId());
MainActivity.favorisList.add(document.getId());
}
}
否则{
Log.d(“标记”,“获取文档时出错:”,task.getException());
}
//不带数据检索的调用
Log.e(“TAG”、“favoris::”+showListContentS(MainActivity.favorisList));
getProductByTagFound();
}
})
.addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Log.e(“TAG”,“error get All favoritis”+e);
}
});
}
这里是我需要的第二个问题:

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();
公共静态void getProductByTagFound(){

对于(int i=0;i如果要在第一个查询完成后立即执行新查询,则需要等待直到第一个查询完成。要解决此问题,您需要使用嵌套查询。换句话说,您需要将第二个查询移动到第一个回调内的
onComplete()内
method。这样,只有在第一个查询完成时,才会执行第二个查询。

我知道,但在我的方法
getProductByTagFound()
中,
onComplete()
位于
for()中
循环,我需要完成循环并在调用另一个方法后执行。因此,您基本上希望在第一次迭代结束后立即执行另一个查询,对吗?以同样的方式,执行第二次查询,依此类推。这是您想要的吗?在这种情况下,您应该为循环中的每个迭代创建一个任务对象,并将其添加到
列表中然后调用
whenAllSuccess()
方法,如我的回答中所述。它是这样工作的吗?这是完全相同的用例,每次调用
get()
时都会返回一个任务对象,所以在每次迭代中,将该任务对象添加到列表中。他们只需调用
whenAllSuccess()
即可。您应该调试该代码(for循环)使用断点查看代码失败的位置。