MyQuestion
MyQuestion

Reputation: 41

Android Firebase onDataChange is not called?

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.

Upvotes: 3

Views: 4130

Answers (2)

oiyio
oiyio

Reputation: 5925

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

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

Upvotes: 0

GraSim
GraSim

Reputation: 4210

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.

Upvotes: 2

Related Questions