Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase - "onDataChange" And "onCancelled" Not Being Called With No Internet Connection

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.

like image 739
Rafi Avatar asked Aug 18 '16 14:08

Rafi


2 Answers

Firebase separates the flow of data events (such as onDataChange()) from other things that might happen. It will only call onCancelled when there is a server-side reason to do so (currently only when the client doesn't have permission to access the data). There is no reason to cancel a listener, just because there is no network connection.

What you seem to be looking for is a way to detect whether there is a network connection (which is not a Firebase-specific task) or whether the user is connected to the Firebase Database back-end. The latter you can do by attaching a listener to .info/connected, an implicit boolean value that is true when you're connected to the Firebase Database back-end and is false otherwise. See the section in the document on detecting connection state for full details.

like image 193
Frank van Puffelen Avatar answered Nov 14 '22 13:11

Frank van Puffelen


Hope that my solution can help somebody else (I assume that you already did something else)

Besides to set the keepSynced to true in my database reference:

databaseRef.keepSynced(true)

And add to my Application class:

FirebaseDatabase.getInstance().setPersistenceEnabled(true)

I've added two listeners, one for offline mode and other one for online:

override fun create(data: BaseObject): Observable<String> {
    return Observable.create { observer ->
        val objectId = databaseRef.push().key
        objectId?.let { it ->
            data.id = it

            val valueEvent = object : ValueEventListener {
                override fun onCancelled(e: DatabaseError) {
                    observer.onError(e.toException())
                }

                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    observer.onComplete()
                }
            }
            // This listener will be triggered if we try to push data without an internet connection
            databaseRef.addListenerForSingleValueEvent(valueEvent)

            // The following listeners will be triggered if we try to push data with an internet connection
            databaseRef.child(it)
                    .setValue(data)
                    .addOnCompleteListener {
                        observer.onComplete()
                        // If we are at this point we don't need the previous listener
                        databaseRef.removeEventListener(valueEvent)
                    }
                    .addOnFailureListener { e ->
                        if (!observer.isDisposed) {
                            observer.onError(e)
                        }
                        databaseRef.removeEventListener(valueEvent)
                    }
        } ?: observer.onError(FirebaseApiNotAvailableException("Cannot create new $reference Id"))
    }
}

To be honest, I don't like the idea to attach two listeners very much, if somebody else has a better and elegant solution, please let me know, I have this code temporarily until I find something better.

like image 2
David Alejandro Burgos Rodas Avatar answered Nov 14 '22 14:11

David Alejandro Burgos Rodas