Nested Firestore Asynchronous Listeners In Android
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 Activiti
Solution 1:
Managed to code what @Doug had suggested. Thanks taskmaster! This is so much better:
db.collection("calendar").get()
.continueWith(new Continuation<Task<QuerySnapshot>, Task<?>>() {
@OverridepublicTask<?> then(@NonNullTask<Task<QuerySnapshot>> task) throwsException {
List<Task<QuerySnapshot>> tasks = new ArrayList<Task<QuerySnapshot>>();
for (DocumentSnapshot ds : task.getResult().getResult())
tasks.add(ds.getReference().collection("thingstodo").get());
returnTasks.whenAllSuccess(tasks);
}
})
.addOnCompleteListener(new OnCompleteListener<Task<?>>() {
@Overridepublic void onComplete(@NonNullTask<Task<?>> task) {
List<QuerySnapshot> lists = (ArrayList<QuerySnapshot>)task.getResult().getResult();
for (QuerySnapshot qs : lists)
for (DocumentSnapshot ds: qs) {
ScheduledItem item = ds.toObject(ScheduledItem.class);
//add to list including day
itemsList.add(item);
}
//list ready to be used!
}
});
Solution 2:
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.
Post a Comment for "Nested Firestore Asynchronous Listeners In Android"