Chris
Chris

Reputation: 6311

Android: Firebase ChildEventListener() not working

I am working in my app with Firebase and I tried to use onChildAdded() callback but I think I am doing something wrong because I get no response.

/**
 * Constructor
 */
private FirebaseManager(Context context) {
    Firebase.setAndroidContext(context);
    Firebase.getDefaultConfig().setPersistenceEnabled(true);
    mFirebaseRef = new Firebase(FIREBASE_URL);
}

public void checkUsers() {
    Firebase checkRef = mFirebaseRef.child("/data/users/");
    // Also tried 
    // Firebase checkRef = mFirebaseRef.child("data").child("users");
    checkRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.d(TAG,"New child added");
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }


    });
}

In onCreate() of my MainActivity I authenticate the user and the call checkUsers(). After that I manually add a new user in database. But I get nothing in log. This is the json structure:

enter image description here

What am I doing wrong?

Upvotes: 4

Views: 2887

Answers (1)

Chris
Chris

Reputation: 6311

I got it. There was a concurrency problem. I called checkUsers() in onCreate() of the MainActivity but it needs to be called in onAuthenticated(), because the user was authenticated after the method checkUsers() tried to set listeners.

I got a LOG about the failure of accessing database:

W/SyncTree: Listen at / failed: FirebaseError: Permission denied

but didn't seen it because I was following a specific TAG with logcat Search.

Upvotes: 1

Related Questions