Identify Data that has been persisted locally but not synchronised in firestore

Viewed 56

If you enable Firebase Persistence, Firebase automatically persists your data to a local cache. But I need to identify data that has been persisted but not synchronized online. And secondly, how do know if the data has been persisted locally? Is there an event of some sort or how do you know when to navigate to a different screen or stop a spinner?

I am implementing this in flutter but your answer doesn't have to be specific to that.

Thank you for your response.

1 Answers

To identify if data is from cache shapshot contain property metadata which is in type SnapshotMetadata which has a property FromCache.

In the documentation there is nice explanation of the feature.

To check whether you're receiving data from the server or the cache, use the fromCache property on the SnapshotMetadata in your snapshot event. If fromCache is true, the data came from the cache and might be stale or incomplete. If fromCache is false, the data is complete and current with the latest updates on the server.

Of course Firebase documentation does not contain Flutter, however in Flutter cloud_firestore library documentation, there is the same property metadata and the similar class SnapshotMetadata.

The only difference I have found is that in Flutter the properly is called isFromCache.

So I think you should use the approach from Firebase tutorial, just converting it to Flutter.

Related