简体   繁体   中英

firebase firestore return wrong data for collection query

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在此处输入图像描述

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.

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