Android Firebase - " onDataChange"而“onCancelled”不被称为没有互联网连接 [英] Android Firebase - "onDataChange" And "onCancelled" Not Being Called With No Internet Connection

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

问题描述

在我的应用程序中,我试着通过在> Firebase 数据库中添加一个 ListenerForSingleValueEvent 以下代码:

  myRef.child(段落)。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);
}

});

当有网络连接时,但是,当我故意关闭互联网连接时,不会调用 onDataChange onCancelled 。这是非常令人沮丧的,因为 databaseError.getCode()中的两个错误代码与网络连接问题有关。

如果因为没有互联网而无法获得这个数据,我想至少让用户知道,而不是让这个监听器挂在屏幕上不断加载。有没有办法解决这个问题?我必须诉诸 Firebase的 REST API吗?至少在使用RESTful网络请求时,他们会告诉您连接是否失败。解析方案

Firebase分隔数据事件流(如 onDataChange())从其他可能发生的事情。当有服务器端原因时(当前只有当客户端没有权限访问数据时),它才会调用 onCancelled 。没有理由取消侦听器,只是因为没有网络连接。



你似乎在寻找的是一种检测是否有网络连接的方法(这不是特定于Firebase的任务),还是用户连接到Firebase数据库的后端。后者你可以通过将一个监听器附加到 .info / connected ,隐式布尔值是 true 已连接到Firebase数据库后端,否则为 false 。有关检测连接状态的信息,请参阅文档中的< a>了解详情。


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.

解决方案

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.

这篇关于Android Firebase - &quot; onDataChange&quot;而“onCancelled”不被称为没有互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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