Java,Firestore – 在Android Studio中使用文档ID的ArrayList从Firestore获取多个文档

huangapple go评论73阅读模式
英文:

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

问题

我有一个包含我想获取的所有 id 的 ArrayList。是否有办法我可以使用该列表获取多个文档及其字段值。另外,如果我必须手动创建一个 for 循环来获取每个文档,应该如何做?

Firestore 数据结构:

Users(用户):
  user1(用户1):
     field1(字段1):
     field2(字段2):
     field3(字段3):
     field4(字段4):
  user2(用户2):
     field1(字段1):
     field2(字段2):
     field3(字段3):
     field4(字段4):
  user3(用户3):
     field1(字段1):
     field2(字段2):
     field3(字段3):
     field4(字段4):

列表包含:user1,user2,user3,依此类推...

编辑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);
                                }
                            });

编辑2:

循环的另一种逻辑也导致相同的错误。当我记录日志时,我可以看到 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));
                                    }
                                }

                            }
                        }
                    });
英文:

I have an ArrayList which contains all the ids I want to fetch. 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?

Firestore data structure:

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...

Edit 1:
I tried to get it using a for loop but it gives a null pointer exception
at FriendsData.add(userDetails);

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

Edit 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

FirebaseFirestore.getInstance().collection(&quot;Users&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                        @Override
                        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; 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));
                                    }
                                }

                            }
                        }
                    });

答案1

得分: 5

当您在DocumentReference对象上调用.get()时,您将获得一个Task<DocumentSnapshot>对象。

> 有没有办法可以使用列表获取多个文档及其字段值。

是的,有办法。解决这个问题的关键是whenAllSuccess(Collection<? extends Task<?>> tasks)方法。在您的特定情况下,代码应该如下所示:

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) {
        // 根据需要处理列表
        for (Object object : list) {
            FamilyMember fm = ((DocumentSnapshot) object).toObject(FamilyMember.class);
            Log.d("TAG", fm.getName());
        }
    }
});

假设在您的FamilyMember类中有一个名为name的属性和一个名为getName()的getter方法,您在Logcat中的结果将是所有家庭成员的姓名。

英文:

When you are calling .get() on a DocumentReference object, you are getting back a Task&lt;DocumentSnapshot&gt; 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&lt;? extends Task&lt;?&gt;&gt; tasks) method. In your particular case, the code should look like this:

List&lt;Task&lt;DocumentSnapshot&gt;&gt; tasks = new ArrayList&lt;&gt;();
for (String doc : list_of_Friends) {
    tasks.add(FirebaseFirestore.getInstance().collection(&quot;Users&quot;).document(doc).get());
}

Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener&lt;List&lt;Object&gt;&gt;() {
    @Override
    public void onSuccess(List&lt;Object&gt; list) {
        //Do what you need to do with your list
        for (Object object : list) {
            FamilyMember fm = ((DocumentSnapshot) object).toObject(FamilyMember.class);
            Log.d(&quot;TAG&quot;, 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.

huangapple
  • 本文由 发表于 2020年9月2日 12:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定