So I have Activities documents in several Days collections and I need to combine all of the Activities in a list. I thought I should loop the collections and then loop the Activities and ended up with the code below which I don't know whether it's the best way to combine multiple collections. Even worse I don't know WHEN my list is ready to be used with all the asynchronous calls. Any advice? Thanks!
db.collection("calendar").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot ds : task.getResult().getDocuments()) {
ds.getReference().collection("thingstodo").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot ds : task.getResult().getDocuments()) {
ScheduledItem item = ds.toObject(ScheduledItem.class);
itemsList.add(item);
}
}
});
}
}
});
The get() method you're using on CollectionReference (which is a subclass of Query) returns a Task that becomes resolved when the document is ready. Instead of adding a listener to that individual Task, collect all the tasks into a List, and pass that to Tasks.whenAllComplete() to respond when the entire set is complete. You can then examine the results of all the tasks there, as needed.
Managed to code what @Doug had suggested. Thanks taskmaster! This is so much better: