firebase firestore return wrong data for collection query

Viewed 33

I am using Firestore db to get collection by the name of the collections array it seems that Firestore return wrong results from another collection when I print the results are not form the collection is db.collection(collectionName).get() method. I didn't see error in the loop also in returned value from collections.Any clue where can be the problem.

   private String[] collections = {"appetizers", "breakfast", "indian", "lunch", "noodles", "salad", "trending"};
    private int n = 0;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    for (int i = 0; i < collections.length; i++) {
        db.collection(collections[i])
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            resultList = new ArrayList<>();
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Map<String, Object> data = document.getData();
                                System.out.println(collections[DashBoardActivity.this.n] + " collection " + (String) data.get("data_name"));

                            }
                            DashBoardActivity.this.recipesMap.put(collections[DashBoardActivity.this.n], resultList);
                            DashBoardActivity.this.n++;

                        } else {
                            System.out.println("Task not successful fire");
                        }
                    }
                });

Edit: adding screenshot of the database enter image description here

1 Answers

If all data are recived, I think it just because order differentce between every time you run .get(), Consider using ref.orderBy(/* field */); to get constant order.

Related