In my application I have a function where I display some data in RecyclerView and for that I need a list of data.
I have a collection (ActiveJobs) and its documents have a field of string array called jobApplicantsId, and have another collection called Repairmen. My task is to list all of the documents from Repairmen collection where the document id is the same as in the specific ActiveJobs document array field.
Actually I did the first half of the task, I can get the corresponding documents, but as you can see the code from below, I'm using a query inside a for loop, which is inside another query! My problem is that I can't get matchingDocs outside the for loop because of async communication.
public void getApplicants(String docID, final GetListCallback callback) {
final List<DocumentSnapshot> matchingDocs = new ArrayList<>();
fStore.collection("ActiveJobs").document(docID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot doc = task.getResult();
ArrayList<String> list = (ArrayList<String>) doc.get("jobApplicantsId");
for (int i = 0; i < list.size(); i++) {
fStore.collection("Repairmen").whereEqualTo("repID", list.get(i)).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot doc : task.getResult()) {
matchingDocs.add(doc);
Log.d("list", matchingDocs.toString()); //getting the right results
}
}
}
});
}
}
Log.d("list", matchingDocs.toString()); //matchingDocs is empty here
}
});
}
I think I have to use Tasks.whenall()... but I can't actually know how can I make this work. Thanks for your time and help!