无法遍历Firebase上的每个孩子

问题描述 投票:0回答:1

我试图遍历数据快照中收到的每个孩子并将密钥放在地图中。在下面的代码片段中,执行似乎停止了我将数据放入地图的位置。

        FirebaseDatabase.getInstance().getReference().child("Fixtures").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if (snapshot.exists()) {

                long childcount = snapshot.getChildrenCount();

                    for (DataSnapshot ds : snapshot.getChildren()) {

                        String currentuser = ds.getKey();
                        List<UserFixtures> emptyList = new ArrayList<>();
                        userPredictions.put(currentuser, emptyList);

                        childcount--;

                        if (childcount == 0) {
                            addFixturestoUserPredictions();
                        }

                }
            }
        }
        @Override
        public void onCancelled (DatabaseError error){

        }
    });

所以有三个子节点,但在第一次迭代中,执行在map.put方法之后停止。它不会崩溃,它只是停止,甚至没有达到childcount--;。我不知道是怎么回事。

快照肯定包含所有三个孩子及其数据。

此代码位于Google App Engine上。

java google-app-engine firebase firebase-realtime-database
1个回答
0
投票

找出问题所在。

我把我的名单宣布为

Map<String, List<UserFixtures>> userPredictions

但我应该......

Map<String, List<UserFixtures>> userPredictions = new HashMap<>();
© www.soinside.com 2019 - 2024. All rights reserved.