简体   繁体   English

Java,Firestore - 在 android 工作室中使用 arraylist 的文档 ID 从 Firestore 获取多个文档

[英]Java, Firestore - Get multiple documents from firestore using an arraylist of document ids in android studio

I have an ArrayList which contains all the ids I want to fetch.我有一个 ArrayList,其中包含我想要获取的所有 ID。 Is there a way I can fetch multiple documents and their field values using the list.有没有一种方法可以使用列表获取多个文档及其字段值。 Also, if I have to manually create a for loop to get each document, how shall I do it?另外,如果我必须手动创建一个for循环来获取每个文档,我该怎么办?

Firestore data structure: Firestore数据结构:

Users:
  user1:
     field1 :
     field2 :
     field3 :
     field4 :
  user2:
     field1 :
     field2 :
     field3 :
     field4 :
  user3:
     field1 :
     field2 :
     field3 :
     field4 :

The list contains: user1,user2,user3 and so on...该列表包含:user1、user2、user3 等等...

Edit 1: I tried to get it using a for loop but it gives a null pointer exception at FriendsData.add(userDetails);编辑 1:我尝试使用 for 循环获取它,但它在FriendsData.add(userDetails);

 for (String doc : list_of_Friends) {
                    FirebaseFirestore.getInstance().collection("Users").document(doc).get()
                            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    FamilyMember userDetails = documentSnapshot.toObject(FamilyMember.class);
                                    FriendsData.add(userDetails);
                                }
                            });

Edit 2:编辑 2:

Another logic for the loop also gives the same error.循环的另一个逻辑也给出了同样的错误。 When I log, I can see that both the doc & ds.getId() are getting values当我登录时,我可以看到docds.getId()都在获取值

FirebaseFirestore.getInstance().collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if(task.isSuccessful())
                            for (DocumentSnapshot ds : task.getResult()){
                                for (String doc : list_of_Friends) {
                                    if(ds.getId().equals(doc)){
                                        FriendsData.add(ds.toObject(FamilyMember.class));
                                    }
                                }

                            }
                        }
                    });

When you are calling .get() on a DocumentReference object, you are getting back a Task<DocumentSnapshot> object.当您在 DocumentReference object 上调用.get()时,您将返回一个Task<DocumentSnapshot> object。

Is there a way I can fetch multiple documents and their field values using the list.有没有一种方法可以使用列表获取多个文档及其字段值。

Yes, it is.是的。 The key to solving this problem is whenAllSuccess(Collection<? extends Task<?>> tasks) method.解决这个问题的关键是whenAllSuccess(Collection<? extends Task<?>> tasks)方法。 In your particular case, the code should look like this:在您的特定情况下,代码应如下所示:

List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String doc : list_of_Friends) {
    tasks.add(FirebaseFirestore.getInstance().collection("Users").document(doc).get());
}

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) {
            FamilyMember fm = ((DocumentSnapshot) object).toObject(FamilyMember.class);
            Log.d("TAG", fm.getName());
        }
    }
});

Assuming that you have in your FamilyMember class a property called name and a getter called getName() , the result in your logcat will be all the names of all family members.假设您的FamilyMember class 中有一个名为name的属性和一个名为getName()的 getter,则您的 logcat 中的结果将是所有家庭成员的姓名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想从 firestore 中的集合中获取多个字段的文档 - I want to get documents for multiple fields from a collection in firestore 无法从 FIRESTORE 的集合中获取多个文档 - UNABLE TO Get multiple documents from a collection FROM FIRESTORE Google Firestore - 如何在一次往返中通过多个 id 获取多个文档? - Google Firestore - How to get several documents by multiple ids in one round-trip? 如何检查在 Java 中的 Firestore 上删除了哪个文档 - Android Studio - How to check which document is deleted on Firestore in Java - Android Studio 从 Firestore 集合中获取包含所有文档的列表 - Get a list with all documents from a Firestore collection 如何从 Firestore 集合 Android Java 中的数组获取数据 - How To Get Data From Array in Firestore Collection Android Java 如何获取集合 Cloud Firestore 中的文档 ID 列表? - How to get a list of document IDs in a collection Cloud Firestore? 为 RecyclerView 获取多个 Firestore 文档 - Fetching Multiple Firestore Documents for RecyclerView 如何在 Firestore 中加入多个文档? - How to join multiple documents in a Firestore? 尝试使用用户配置文件中的数组一次从 React 中的 Firestore 查询多个文档 - Trying to query multiple documents from Firestore in React at once, using an array from the users profile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM