简体   繁体   中英

How to retrieve data from merge tasks in firestore query?

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();

                }
            }
        }
    });

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() .

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