【问题标题】:Can't iterate through every child on Firebase无法遍历 Firebase 上的每个孩子
【发布时间】:2018-06-10 15:39:12
【问题描述】:

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

        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 上。

【问题讨论】:

  • 可以分享日志吗?为什么要向地图添加空列表 (List emptyList = new ArrayList(); userPredictions.put(currentuser, emptyList);)?

标签: java google-app-engine firebase firebase-realtime-database


【解决方案1】:

找出问题所在。

我已将我的列表声明为

Map<String, List<UserFixtures>> userPredictions

但我应该……

Map<String, List<UserFixtures>> userPredictions = new HashMap<>();

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 2012-09-04
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多