简体   繁体   English

根据文档列表查询 FireStore

[英]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.

相关问题 Firestore 查询文档以字符串开头 - Firestore query documents startsWith a string Firestore通过字符串键查询文档 - Firestore Query documents by string key 如果我使用Query在“Android”中拥有用户ID arraylist,如何从“Firestore”集合中获取特定的文档列表 - How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query 如何避免在Firestore查询中出现空文档? - How to avoid empty documents in a Firestore query? 查询Firestore数据并将一个字段添加到与查询匹配的所有文档中 - Query Firestore data and add one field to all the documents that match the query 如何在Elasticsearch中创建文档子集并针对该子集执行查询? - How to create a subset of documents and execute a query against the subset in Elasticsearch? 如何根据查询找到相关文档 - How do I find relavence documents against the query 检查是否没有针对Firebase ui查询类返回任何文档 - Check if no documents are returned against a Firebase ui query class 如何计算 Cloud Firestore 中查询过滤的文档数 - How to count the number of documents filtered by a query in Cloud Firestore 在文档中搜索子字符串时,较低的 Firestore 查询读取 - lower firestore query reads when searching substring in documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM