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

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

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

问题

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

Firestore 数据结构:

  1. Users(用户):
  2. user1(用户1):
  3. field1(字段1):
  4. field2(字段2):
  5. field3(字段3):
  6. field4(字段4):
  7. user2(用户2):
  8. field1(字段1):
  9. field2(字段2):
  10. field3(字段3):
  11. field4(字段4):
  12. user3(用户3):
  13. field1(字段1):
  14. field2(字段2):
  15. field3(字段3):
  16. field4(字段4):

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

编辑1:
我尝试使用 for 循环获取它,但在 FriendsData.add(userDetails); 处出现空指针异常。

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

编辑2:

循环的另一种逻辑也导致相同的错误。当我记录日志时,我可以看到 docds.getId() 都有值。

  1. FirebaseFirestore.getInstance().collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  2. @Override
  3. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  4. if(task.isSuccessful())
  5. for (DocumentSnapshot ds : task.getResult()){
  6. for (String doc : list_of_Friends) {
  7. if(ds.getId().equals(doc)){
  8. FriendsData.add(ds.toObject(FamilyMember.class));
  9. }
  10. }
  11. }
  12. }
  13. });
英文:

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:

  1. Users:
  2. user1:
  3. field1 :
  4. field2 :
  5. field3 :
  6. field4 :
  7. user2:
  8. field1 :
  9. field2 :
  10. field3 :
  11. field4 :
  12. user3:
  13. field1 :
  14. field2 :
  15. field3 :
  16. 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);

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

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

  1. FirebaseFirestore.getInstance().collection(&quot;Users&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
  2. @Override
  3. public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
  4. if(task.isSuccessful())
  5. for (DocumentSnapshot ds : task.getResult()){
  6. for (String doc : list_of_Friends) {
  7. if(ds.getId().equals(doc)){
  8. FriendsData.add(ds.toObject(FamilyMember.class));
  9. }
  10. }
  11. }
  12. }
  13. });

答案1

得分: 5

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

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

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

  1. List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
  2. for (String doc : list_of_Friends) {
  3. tasks.add(FirebaseFirestore.getInstance().collection("Users").document(doc).get());
  4. }
  5. Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
  6. @Override
  7. public void onSuccess(List<Object> list) {
  8. // 根据需要处理列表
  9. for (Object object : list) {
  10. FamilyMember fm = ((DocumentSnapshot) object).toObject(FamilyMember.class);
  11. Log.d("TAG", fm.getName());
  12. }
  13. }
  14. });

假设在您的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:

  1. List&lt;Task&lt;DocumentSnapshot&gt;&gt; tasks = new ArrayList&lt;&gt;();
  2. for (String doc : list_of_Friends) {
  3. tasks.add(FirebaseFirestore.getInstance().collection(&quot;Users&quot;).document(doc).get());
  4. }
  5. Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener&lt;List&lt;Object&gt;&gt;() {
  6. @Override
  7. public void onSuccess(List&lt;Object&gt; list) {
  8. //Do what you need to do with your list
  9. for (Object object : list) {
  10. FamilyMember fm = ((DocumentSnapshot) object).toObject(FamilyMember.class);
  11. Log.d(&quot;TAG&quot;, fm.getName());
  12. }
  13. }
  14. });

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:

确定