Android Firebase onDataChange is not called?

↘锁芯ラ 提交于 2019-12-29 01:45:08

问题


I have more than 40,000 nodes in my Firebase Database. But whenever I am trying to read a key from my Android code, the onDataChange method is not getting called.

MyCode:

 Firebase firebase = new Firebase(my url);
            firebase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    Log.d(TAG, "onDataChange : " + dataSnapshot);

                    if (dataSnapshot.getValue() != null) {

                        Map map = (Map) dataSnapshot.getValue();

                        Iterator accountIterator = map.entrySet().iterator();
                        while (accountIterator.hasNext()) {

                            Map.Entry accountPair = (Map.Entry) accountIterator.next();

                            Log.d(TAG, "accountPair : Key : " + accountPair.getKey() + " , Value : " + accountPair.getValue());
                        }

                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    Log.d(TAG, "onCancelled : " + firebaseError);
                }
            });

I want to retrieve Student node details

Please let me know. I am not able to find any issue in the code. Please suggest me some solution.


回答1:


First thing to do is to check you have correct permissions to read from your database. Go to https://console.firebase.google.com/project/[your_database_url]/database/rules and check . Not ideal in prod, but for testing you can set read to true

  {
  "rules": {
     ".read": true,
     ".write": "auth != null"
   }
  }

Next gotcha is to remember that the listeners are asynchronous fire and forget calls. So any manipulations to the data should be done once the data has been received in the onDataChange() method.




回答2:


Open Firebase link -> Database -> Rules -> Probably values here are false in your case. Make it true as the following.

  {
    "rules": {
      ".read": true,
      ".write": true
    }
  }


来源:https://stackoverflow.com/questions/39851052/android-firebase-ondatachange-is-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!