0

How do I know where many SnapshotListener of a different collections has been completed?

Take an example of trying to retrieve a user from a user collection, a team from the teams collection, and a coach from the coaches collection.

I want to make the activity "active" only when i got all the data that I am looking for.

For example:

  firebaseFirestore.collectionGroup("users").whereEqualTo("user_id", currentUser.getId()).addSnapshotListener(this, new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                   // getting the user
                    }

  firebaseFirestore.collectionGroup("teams").whereEqualTo("team_id", currentTeam.getId()).addSnapshotListener(this, new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                   // getting the team
                    }


  firebaseFirestore.collectionGroup("coachs").whereEqualTo("coach_id", currentCoach.getId()).addSnapshotListener(this, new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                   // getting the coach
                    }

How do I make sure that only when I got all the three i will make my activity "enable"?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
DVIR
  • 51
  • 3
  • I could make a three booleans in my viewmodel that represnts if the data is ready for each snapshot, and on each complete listiner update the value and call to a synchronized method that looks for the three booleans and if there are all true, update the activity. But is there more "elegant" way of doing that? – DVIR Oct 17 '19 at 06:42

1 Answers1

1

You cannot know when getting the data from the database using addSnapshotListener() has completed becase Cloud Firestore is a realtime database and getting data might never complete. That's why is named a realtime database because in any momemnt the database can be changed, items can be added or deleted.

The only way to partially know if you have all the data from a particular query is to perform a get() call on that query. Even then, the data may change after that listener is invoked, so all you really have is a snapshot at a particular moment in time.

In your particular case, you can use get() in case of each query and save that in a Task object. To know when all tasks are complete, please use my answer from the following post:

But instead of using two queries, you should use three.

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

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