Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase onDataChange not called

I am working with Firebase for my Android app. I have successfully retrieved information in multiple places in my code using something like:

dbRef = new Firebase(Constants.URL_DB);
childRef = dbRef.child(Constants.KEY_CHILD);
objectRef = childRef.child(objectId); 

// Log the URL to the object.
// This has been verified. The path is good.
Log.d("DEBUG", "To String: " + objectRef.toString()); 

childRef.addValueEventListener(new ValueEventListener()
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        Log.d("DEBUG", "Success");
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.e("DEBUG", "Failure");
    }
});

The exact same code has been use successfully in another activity but for some reason, it doesn't work in on particular activity (onDataChange is never called, neither is onCancelled). Could it be that The Firebase sessions in other activities are conflicting with this one? I have seen problems that could be resolved with:

Firebase.goOnline();
Firebase.goOffline();

Althought I am not sure I understand what those exactly do. Maybe it is because I somehow can't access a child added by push() with his id?

UPDATE:

I successfully wrote in the database at objectRef, but onDataChange() is still never called.

like image 356
Sunshinator Avatar asked Nov 08 '22 19:11

Sunshinator


1 Answers

Ok I found what was causing the problem. I had an empty loop because I needed the information to continue and it somehow prevented the events from the database. I had something like this in the onCreate():

childRef.addValueEventListener(new ValueEventListener()
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        Log.d("DEBUG", "Success");
        object = snapshot.getValue(MyObject.class);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.e("DEBUG", "Failure");
    }
});

And I had the loop in the onResume():

while(object == null);

What I did to correct the issue was replacing the loop with:

if(object == null){
    // Do stuff
}

And I added a call to onResume() in onDataChange()

like image 190
Sunshinator Avatar answered Nov 14 '22 22:11

Sunshinator