英文:
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:
循环的另一种逻辑也导致相同的错误。当我记录日志时,我可以看到 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));
}
}
}
}
});
英文:
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("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:
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("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));
}
}
}
}
});
答案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<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. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论