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

后端 未结 2 618
情书的邮戳
情书的邮戳 2020-12-11 02:01

In my app, I simply try to retrieve a reading passage from my Firebase database by adding a ListenerForSingleValueEvent in the following code:

2条回答
  •  既然无缘
    2020-12-11 02:49

    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.

提交回复
热议问题