Firebase addValueEventListener有时会返回null DataSnapshot

时间:2015-05-25 19:30:52

标签: android firebase

我的Android应用程序使用了功能非常好的Firebase JAVA API。但是,我最近注意到,有时我使用此代码获取数据快照中返回的空值。

 public void startLeaderboardListener (final String roundID) {
    myClubFirebaseRef.child("/rounds/" + roundID + "/scores/users/").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            JSONArray usersJSONArray = new JSONArray();
            //ToDo : BUG : Returned a null object reference here
            Map<String, JSONObject> usersMap = (HashMap<String, JSONObject>) snapshot.getValue();
            JSONObject usersJSON = new JSONObject(usersMap);
            if (usersMap != null) try {
                Iterator x = usersJSON.keys();
                while (x.hasNext()) {
                    String thisUSerID = (String) x.next();
                    usersJSONArray.put(usersJSON.get(thisUSerID));

                    JSONObject thisUserJSON = usersJSON.getJSONObject(thisUSerID);
                    SQLiteRounds db = new SQLiteRounds(myContext);
                    JSONObject thisUserStatusJSON = thisUserJSON.getJSONObject("status");
                    JSONArray thisUserDataJSON = thisUserJSON.getJSONArray("data");
                    db.updateLocalRoundwithScores(thisUSerID, roundID, thisUserStatusJSON, thisUserDataJSON);
                }
                myScoreListener.onScoreUpdated(usersJSONArray);

            } catch (Throwable t) {
                t.printStackTrace();
                Log.e ("CloudArchery", "Error extracting JSON Data from User Update (ClubFirebase.startLeaderboard");
            }
        }//StartLeaderboard

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("CloudArchery", "The read failed (ClubFirebase.startLeaderboard) : " + firebaseError.getMessage());
        }

    });
} //startLeaderboardListene

0 个答案:

没有答案