4

I have this code that fetch data from specific sub-collection:

 db.collection("groups").document(id.get(i)).collection("members")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                id.add(document.getId());
                                Log.e(Tag, document.getId() + " => " + document.getData());

                                if (task.getResult().isEmpty()) {
                                    Log.d(Tag, "onSuccess: LIST EMPTY");
                                    return;
                                } else {
                                    // Convert the whole Query Snapshot to a list
                                    // of objects directly! No need to fetch each
                                    // document.
                                    Log.e(Tag, task.getResult() + "");
                                    typeAll = task.getResult().toObjects(GroupMembers.class);
                                }
                            }
                        } else {
                            Log.e(Tag, "Error getting documents: ", task.getException());
                        }

But I need to call a method after fetching all the data by snapshots. How to achieve that? what should I test?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Reeman Adel
  • 51
  • 1
  • 7

2 Answers2

4

How to know that Snapshot Listener is finished?

The Snapshot Listener is always listening for changes, unless you are removing it.

If you are looking to know when the data is completed loading from the database, note that you cannot know when all the members are completed downloaded becase Cloud Fireatore is a realtime database and getting data might never complete. That's why is named a realtime database because in any momemnt the data under the members collection can be changed, properties can be added or deleted.

You can use a CompletionListener only when you write or update data and you'll be notified when the operation has been acknowledged by Firebase servers but you cannot use this interface when reading data.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

Call your method to process the object list after you retrieve it:

                           if (task.isSuccessful()) {
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    id.add(document.getId());
                                    Log.e(Tag, document.getId() + " => " + document.getData());

                                    if (task.getResult().isEmpty()) {
                                        Log.d(Tag, "onSuccess: LIST EMPTY");
                                        return;
                                    } else {
                                        // Convert the whole Query Snapshot to a list
                                        // of objects directly! No need to fetch each
                                        // document.
                                        Log.e(Tag, task.getResult() + "");
                                        typeAll = task.getResult().toObjects(GroupMembers.class);

                                        // do something with typeall here

                                    }
                                }
                            } else {
                                Log.e(Tag, "Error getting documents: ", task.getException());
                            }
live-love
  • 48,840
  • 22
  • 240
  • 204