[英]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当我登录时,我可以看到
doc
和ds.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.