Android Firebase - “onDataChange"和“onCancelled"在没有互联网连接的情况下不被呼叫 [英] Android Firebase - "onDataChange" And "onCancelled" Not Being Called With No Internet Connection

查看:17
本文介绍了Android Firebase - “onDataChange"和“onCancelled"在没有互联网连接的情况下不被呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我只是尝试通过在以下代码中添加 ListenerForSingleValueEvent 来从我的 Firebase 数据库中检索阅读段落:

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);
            }

        });

当有互联网连接时它工作得很好.但是,当我故意关闭互联网连接时,onDataChangeonCancelled 都没有被调用.这非常令人沮丧,因为 databaseError.getCode() 中的两个错误代码与网络连接问题有关.

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.

如果我因为没有互联网而无法获得这些数据,我至少想让用户知道,而不是让这个监听器挂在屏幕上不断加载.有没有办法解决这个问题?我是否只需要求助于 Firebase 的 REST API?至少对于 RESTful 网络请求,它们会让您知道连接是否失败.

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.

推荐答案

Firebase 将数据事件流(例如 onDataChange())与其他可能发生的事情分开.它只会在有服务器端原因时调用 onCancelled(目前仅当客户端无权访问数据时).没有理由取消监听器,只是因为没有网络连接.

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.

您似乎在寻找一种方法来检测是否存在网络连接(这不是 Firebase 特定的任务)或用户是否连接到 Firebase 数据库后端.后者您可以通过将侦听器附加到 .info/connected 来实现,这是一个隐式布尔值,当您连接到 Firebase 数据库后端时为 true 并且是false 否则.请参阅文档中有关检测连接状态的部分 了解完整详情.

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.

这篇关于Android Firebase - “onDataChange"和“onCancelled"在没有互联网连接的情况下不被呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆