0

please can someone tell me how to retrieve all three tasks? Actually, I'm able to retrieve only two tasks.

CollectionReference players = db.collection("gamers");

    Task task1 = players.whereEqualTo("player_id_one", myId)
            .get();

    Task task2 = players.whereEqualTo("player_id_two", myId)
            .get();
Task task3 = players.whereEqualTo("player_id_three",myId).get();

    Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2,task3);
    allTasks.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() {
        @Override
        public void onSuccess(List<QuerySnapshot> querySnapshots) {
            for (QuerySnapshot queryDocumentSnapshots : querySnapshots) {
                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    Modelgame players = documentSnapshot.toObject(Modelgame.class);
                    result.add(modelplayer);
                    adapter.notifyDataSetChanged();

                }
            }
        }
    });
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • you can give a try by adding `task4` and then you may get 3 instead of 4 – Vishal Beep May 01 '22 at 08:00
  • Oh I see.. but i have no idea how I'm supposed to arrange the for loop so that it display the results. If you have any ideas please your welcome – freeday same May 01 '22 at 08:04
  • Can you tell me what output or error you are getting in your case. and what output or error after adding `task4`. – Vishal Beep May 01 '22 at 08:08
  • Actually no error occurred. My code retrieve only two tasks but when i add the third and the fourth task. Nothing display in my recyclerView . – freeday same May 01 '22 at 08:39
  • 1
    thank you I saw the problem firestore asked me to add an index query in my console ‍♂️.. It finally works – freeday same May 01 '22 at 14:25

1 Answers1

1

When you're using Tasks#whenAllSuccess(Task...<?> tasks) method it:

Returns a Task with a list of Task results that completes successfully when all of the specified Tasks complete successfully.

This means that the List you're getting is not a List<QuerySnapshot> but a List<Object>:

allTasks.addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> querySnapshots) {
        //Loop through the list only once
        for (Object object : querySnapshots) {
            Modelgame players = ((DocumentSnapshot) object).toObject(Modelgame.class);
            result.add(modelplayer);
            Log.d("TAG", players.getName());
        }
        adapter.notifyDataSetChanged(); //Added when the for loop ends.
    }
});

So once all the tasks are successful you only need to loop through the results once and cast the object to a DocumentSnapshot object and call toObject().

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • i just tried it . My recyclerView display nothing it remains blank – freeday same May 01 '22 at 08:50
  • Does this `Log.d("TAG", players.getName());` pint something in the logcat? If you don't have a getter for the name, then use another one. Besides that, please edit your question and add your database structure as a screenshot. Have you also tried to use a hard-coded value for `myId`? – Alex Mamo May 01 '22 at 09:21
  • I tried the code again but this time an error occurred. Java.lang.Class.CastException: com.google.firebase.firestore.QuerySnapshot cannot be cast to com.google.firebase.firestore.DocumentSnapshot – freeday same May 01 '22 at 13:50
  • thank you I saw the problem firestore asked me to add an index query in my console ‍♂️.. It finally works – freeday same May 01 '22 at 14:24
  • actually yes. Check internet connection.. check internet bandwidth – freeday same May 08 '22 at 13:16
  • actually yes. is there a firestore or firebase code to check the internet connection? – freeday same May 08 '22 at 13:24
  • For that, I recommend you check this [answer](https://stackoverflow.com/questions/52279144/how-to-verify-if-user-has-network-access-and-show-a-pop-up-alert-when-there-isn). So like the answer states, you can use `.isFromCache()`, right? – Alex Mamo May 09 '22 at 09:04
  • Can I help you with other information, other than that? – Alex Mamo May 09 '22 at 09:05