[英]Query FireStore against a List of Documents
I have a List<String>
of names referring to Documents that I want to retrieve from FireStore.我有一个List<String>
名称引用我想从 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.我想在加载完成后访问内容,因此我在使用数据的Fragment
中实现了一个OnCompleteListener
。 However, I am not sure how to run a loop within a Task
to query FireStore for each Document.但是,我不确定如何在Task
运行循环来查询每个文档的 FireStore。 I am querying FireStore in a Repository class that returns a Task
object back through my ViewModel
and finally to my Fragment
.我在 Repository 类中查询 FireStore,该类通过我的ViewModel
返回一个Task
对象,最后返回到我的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.我希望存储库返回一个Task
以便我可以将一个OnCompleteListener
附加到它,以便知道我何时完成加载我的数据。
My Repository Query
method:我的存储库Query
方法:
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.我知道这不会返回我需要的东西,但我不确定如何构建一个在其中包含循环的Task
。 Would I need to extract the contents of the List<String>
in my Fragment
and run individual queries for each item?我是否需要提取Fragment
List<String>
的内容并为每个项目运行单独的查询?
Any suggestions would be greatly appreciated.任何建议将不胜感激。
According to your comment:根据您的评论:
I have a List of Document names and I need to transform this to essentially a List of Tasks to retrieve the entire document.我有一个文档名称列表,我需要将其转换为本质上的任务列表以检索整个文档。
To solve this, please use the following lines of code:要解决此问题,请使用以下代码行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("yourCollection");
List<String> myGroupTags = new ArrayList<>();
List<DocumentReference> listDocRef = new ArrayList<>();
for(String s : myGroupTags) {
DocumentReference docRef = collRef.document(s);
listDocRef.add(docRef);
}
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : listDocRef) {
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
for (Object object : list) {
GroupBase gb = ((DocumentSnapshot) object).toObject(GroupBase.class);
Log.d("TAG", tp.getPropertyname);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.