我有以下方法:
public static void getRecipe(DocumentReference DR){
Recipe r = new Recipe();
/*First Firestore Query! - Get the recipe*/
DR.get().addOnCompleteListener(task -> {
if (task.isSuccessful() && task.getResult() != null) {
r.addStuff(...);
r.addMoreStuff(...);
r.addEvenMoreStuff(...);
/*Second Firestore Query(s)! - Get the ingredient(s)*/
for(DocumentReference DR_ingredient : ingredients) {
Task<DocumentSnapshot> IngredientQuery = DR_ingredient.get();
IngredientQuery.addOnCompleteListener(t -> {
if (t.isSuccessful() && t.getResult() != null) {
/*even more stuff*/
}
});
}
//After all tasks are finished, we can finally put the recipe in our Database!
Task<Void> t = Tasks.whenAll(IngredientQueries);
t.addOnSuccessListener(unused -> {
addRecipe(r);
});
}
});
}
该方法使用Firestore来获取保存的食谱,并进一步加载所选食谱的配料。这些成分将保存为配方中的参考,并且必须根据Firebase重新加载。
我现在的问题是:如果我从另一个类中调用getRecipe(DocumentReference DR)
,我如何才能等到所有内容加载完毕然后继续执行我的代码?