Firebase onDataChange不在脱机时调用 [英] Firebase onDataChange not called when offline

查看:138
本文介绍了Firebase onDataChange不在脱机时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当手机连接到互联网时,它可以正常工作。但是,当它离线时,它不会调用onDataChange。



这段代码即使处于脱机状态也很好。

  mDatabase = FirebaseDatabase.getInstance()
.getReference()
.child(Database.PLACES)
.child(((BaseActivity) getActivity())getUserId())。

Timber.d(试图获得地方);
mDatabase.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Timber.d(GOT PLACES);
}
$ b @Override
public void onCancelled(DatabaseError databaseError){
Timber.w(databaseError.toException());
}
});

它给了我这个日志:

 D / Persistence:启动事务。 
D / Persistence:在1ms
D / Persistence中保存新的追踪查询。交易完成。经过:3ms
D / EventRaiser:提升1个事件
D / EventRaiser:提升/位置/ JLC2zqpSMFfYEGaUAfSTCHA2K4k1:值:null
D / PlaceListFragment:211:GOT地点
然而,使用完全相同的代码,但改变了DatabaseReference(路径),它给了我这个日志,而不是


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ D $ / $持续性:
D / Persistence:以0ms保存新的跟踪查询
D / Persistence:在1ms中加载总共0个节点的总数为0的节点(Query:1ms,Loading:0ms ,序列化:0ms)
D /持久性:交易完成。什么使我相信有什么东西可以阻止这个事件被称为偶数的如果交易完成的话。

可能发生什么情况?



Obs:我的用户使用Firebase身份验证correclty,我已经配置了我的应用程序设置:

$ p $ FirebaseDatabase.getInstance()。setPersistenceEnabled(true);
FirebaseDatabase.getInstance()。setLogLevel(Logger.Level.DEBUG);


解决方案

这意味着Firebase正在从其缓存中返回数据。由于它以空的快照触发,显然你试图读取的位置从来没有被缓存。因此,它不能调用 onDataChange


$ b $ < onCancelled 方法会被触发。客户根本不知道你是否有权限(因为它没有连接到服务器),所以它不能调用 onCancelled



因此,如果不知道该位置的值(如果有)或者您是否有权限,则没有任何方法会触发。一旦你重新连接到后端,正确的事件将被解雇。



如果你知道这是一个新的位置,想通过写 null prime 那个位置。这样缓存将包含一个值的位置和 onDataChange 将被调用一个没有值的快照。


I am having a very weird situation regarding Firebase ValueEventListener.

When the phone is connected to the internet, it works fine. But when it is offline, it is not calling onDataChange.

This piece of code works great even when it is offline.

mDatabase = FirebaseDatabase.getInstance()
            .getReference()
            .child(Database.PLACES)
            .child(((BaseActivity) getActivity()).getUserId());

Timber.d("TRYING TO GET PLACES");
mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Timber.d("GOT PLACES");
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Timber.w(databaseError.toException());
    }
});

And it gives me this log:

D/PlaceListFragment:207: TRYING TO GET PLACES
D/Persistence: Starting transaction.
D/Persistence: Saved new tracked query in 1ms
D/Persistence: Transaction completed. Elapsed: 3ms
D/EventRaiser: Raising 1 event(s)
D/EventRaiser: Raising /places/JLC2zqpSMFfYEGaUAfSTCHA2K4k1: VALUE: null
D/PlaceListFragment:211: GOT PLACES

However, with the exactly the same code, but changing the DatabaseReference (the path) it gives me this log and does not work.

D/AnalysisActivity$2$override:144: TRYING TO GET READS
D/Persistence: Starting transaction.
D/Persistence: Saved new tracked query in 0ms
D/Persistence: Loaded a total of 0 rows for a total of 0 nodes at /reads/-KdiPFBrnEBHaPGCbP86 in 1ms (Query: 1ms, Loading: 0ms, Serializing: 0ms)
D/Persistence: Transaction completed. Elapsed: 13ms

What makes me believe that there is something that can be preventing this event to be called even if the transaction is completed.

What can be happening?

Obs: My user is authenticated with firebase auth correclty and I have this configured on my application setup:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG);

解决方案

Nothing in the code you shared writes data. This means that Firebase is returning data from its cache. Since it fires with an empty snapshot, apparently the location that you're trying to read was never cached. For this reason it cannot invoke an onDataChange.

The onCancelled method will be triggered when you don't have permission. The client simply doesn't know whether you have permission (since it's not connected to the server), so it cannot invoke onCancelled either.

So without knowledge of the value (if any) at the location or whether you have permission, none of the methods will fire. Once you reconnect to the back-end, the correct event will be fired.

If you know that this is a new location, you might want to prime that location by writing null to it. That way the cache will contain a value for the location and onDataChange will be invoked with a snapshot without a value.

这篇关于Firebase onDataChange不在脱机时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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