简体   繁体   中英

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. 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));
                                    }
                                }

                            }
                        }
                    });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM