我对 Java 和 firestore 很陌生,一直试图从一组 Id 中进行查询,下面是我的代码片段
private void flagCustomer (ArrayList<String> historyList, long time, String originalId){
DocumentReference historyRef;
int completeCount = 0;
for(int i = 0; i < historyList.size(); i++){
historyRef = fStore.collection("history").document(historyList.get(i));
historyRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
//do something...
} else {
Log.d("success", "No such document");
}
} else {
Log.w("failed", "get failed with ", task.getException());
}
}
});
}
}
基本上我有一个 historyList
数组,其中包含类似 [id1,id2,id3]
的内容,我想使用这些 ID 在我的 Firestore 中检索特定数据。
问题是程序总是在查询完成之前结束循环,有没有办法让程序等待我的查询完成,或者有没有更好的方法来做查询而不是把它放在一个循环?