In my app, I simply try to retrieve a reading passage from my Firebase
database by adding a ListenerForSingleValueEvent
in the following code:
myRef.child("passages").child(passageNum).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("ON DATA CHANGE");
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("DATABASE ERROR");
FirebaseErrorHandler.handleDatabaseError(databaseError.getCode(), ReadingActivity.this);
}
});
It works perfectly fine when there is internet connection. However, when I purposely turn off internet connection, neither onDataChange
nor onCancelled
are being called. This is very frustrating since two of the error codes in databaseError.getCode()
have to do with network connectivity issues.
If I can't get this data due to no internet, I want to at least let the user know that instead of having this listener hanging with the screen constantly loading. Is there a way to solve this? Would I have to just resort to Firebase's
REST API? At least with RESTful network requests, they let you know if the connection failed or not.