Firebase卡在没有INTERNET连接上 [英] Firebase gets stuck on no INTERNET connection

查看:130
本文介绍了Firebase卡在没有INTERNET连接上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在使用Firebase来存储数据库.现在,当我在侦听特定位置的子节点中的任何更改时,如果没有Internet连接,则不会触发任何回调. Firebase呼叫只是卡住了.

In my app, I am using Firebase for database. Now when I am listening for any change in child nodes of a particular location, and if there is no internet connection, no callback gets fired. The firebase call just gets stuck.

我正在使用它,

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
firebaseDatabase.setPersistenceEnabled(true);

因此,启用了持久性.该问题仅在一种情况下发生.如果没有有关Firebase本地持久性的数据,并且Internet连接也不可用.

So, persistence is enabled. The problem occurs only one one scenario. When there is no data on the local persistence of Firebase and the internet connection is also not available.

我正在使用它,

addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Timber.d("Cancelled");
                        }
                    });

在该特定情况下,我没有收到两个回调中的任何一个.

I am not getting any of the two callbacks for that particular scenario.

我应该怎么做才能解决这种情况?

What should I do to tackle this kind of scenario?

推荐答案

让我们看看在没有连接的情况下如何触发这些方法:

Let's see how those methods are triggered when there is no connectivity:

onCancelled-发生服务器端错误时.例如,当用户无权访问指定的节点时. (或者当您达到Sparkle计划的连接限制时.)

onCancelled - when there is a server-side error. For example, when the user doesn't have access to the specified node. (Or when you reach the connection limit on the Sparkle plan).

onDataChange-如果有数据保留,它将读取此数据.如果没有,则不会触发此方法(您的情况).

onDataChange - if there is data persisted, it will read this data. If not, this method won't be triggered (your case).

根据 Firebase文档 ,如果要检查设备是否已连接到Firebase服务器,则可以将侦听器添加到.info/connected.像这样:

According to the Firebase Documentation, If you want to check if the device is connected to the Firebase Server, you can add a listener to .info/connected. Like this:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      Toast.makeText(this, "Connected", Toask.LENGTH_SHORT);
    } else {
      Toast.makeText(this, "Not connected", Toask.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onCancelled(DatabaseError error) {
  }
});

这篇关于Firebase卡在没有INTERNET连接上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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