I have a List<String>
of names referring to Documents that I want to retrieve from FireStore. I want to access the contents after they are finished loading so I have implemented an OnCompleteListener
in the Fragment
that uses the data. However, I am not sure how to run a loop within a Task
to query FireStore for each Document. I am querying FireStore in a Repository class that returns a Task
object back through my ViewModel
and finally to my Fragment
. I want the Repository to return a Task
so that I can attach an OnCompleteListener
to it in order to know when I have finished loading my data.
My Repository Query
method:
public Task<List<GroupBase>> getGroups(List<String> myGroupTags){
final List<GroupBase> myGroups = new ArrayList<>();
for(String groupTag : myGroupTags){
groupCollection.document(groupTag).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
myGroups.add(task.getResult().toObject(GroupBase.class));
}
}
});
}
return null; //Ignore this for now.
}
I know this won't return what I need but I am not sure how to structure a Task
that incorporates a loop inside of it. Would I need to extract the contents of the List<String>
in my Fragment
and run individual queries for each item?
Any suggestions would be greatly appreciated.