In my app, I simply try to retrieve a reading passage from my Firebase
database by adding a ListenerForSingleValueEvent
in the following code:
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.