Firebase onDataChange not called when offline

拟墨画扇 提交于 2019-12-24 03:00:53

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/42430992/firebase-ondatachange-not-called-when-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!