Pipiks Pipiks - 3 months ago 14
Android Question

How to know if data are retrieved from local storage or server storage?

At the launch of my application I enabled the data persistence :

FirebaseDatabase.getInstance().setPersistenceEnabled(true);


When my device is offline for example, firebase retrieve data from local storage.

Is there a way to know if data are retrieved from local storage or server storage ? (Without test my network state)

mDatabase.child("users").child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Know if data are retrieved from local storage or server storage
}

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


Thank you.

Answer

There is currently no way to know if the Firebase Database delivers data that comes from the local cache or from the server. If you attach a listener, like you have above: the data will typically be in sync with the server, unless you're not connected (a case you can detect by listening for - as you already - .info/connected).

It is typically best to work with the database in a way that doesn't require knowledge on data freshness. So think of the data as "the freshest data that your app knows off".