caiolopes
caiolopes

Reputation: 571

Firebase onDataChange not called when offline

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

Upvotes: 3

Views: 1075

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

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.

Upvotes: 1

Related Questions