My app uses Firebase to sync and restore data. I use the setValue:withCompletionBlock:
method to insert, update and delete Firebase objects. This method is called w
You can use the snap.metadata.fromCache
flag to check if the value is coming from the cache or not. Note that if the cache value and the value from the server match it won't fire your onSnapshot twice unless you add the includeMetadataChanges
flag seen below, instead it would only fire it once with the metadata.fromCache
flag being set to true
.
db.collection('users').doc(uid).onSnapshot({ includeMetadataChanges: true }, (snap) => {
if (!snap.metadata.fromCache) {
const user = snap.data();
// Code here
}
})