UndyingJellyfish
UndyingJellyfish

Reputation: 310

Retreiving a datasnapshot from Firebase reference

I'm trying to retrieve some data from my public Firebase database, wherein I store a simple highscore list. My problem is that onDataChange() is never actually called or fails to do anything.

Below is part of my code, which is called in a method that runs every time a certain activity is created:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    Log.i(TAG_FIREBASE_READER_HIGHSCORE , ref.child("highscore-list").toString());

    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.i(TAG_FIREBASE_READER_HIGHSCORE , "entered onDataChange for FirebaseReader");
            Toast.makeText(fromContext, "Reading online highscores, please wait...", Toast.LENGTH_LONG).show();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Log.i(TAG_FIREBASE_READER_HIGHSCORE, "found child " + snapshot.toString());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i(TAG_FIREBASE_READER_HIGHSCORE, "onCancelled was called");
        }

    });

In my logcat output I get that onCancelled is called, but not onDataChange. I have confirmed that the reference is right, by getting the reference to spit out the child "highscore-list" in the logcat, which provides the correct output (I can resolve it).

I'm not using any kind of Firebase authentication, so my database rules look like this:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": true,
        ".write": true
      }
    }
  }
}

How do I actually retrieve any data from my database if this doesn't work?

Upvotes: 0

Views: 738

Answers (1)

Martin De Simone
Martin De Simone

Reputation: 2148

  • Make sure you have the internet connection permission

  • Firebase automatically denies acess if you dont specificy security rules for a path, so if you are not reading data from a user in with your security rules, it will fail

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

Upvotes: 1

Related Questions