Ignore local changes in Firebase snapshot listener

Viewed 60

I want my app to sync data and changes between devices. It's easy - you create a snapshot listener and update your data every time it's changing. My problem is that I have a sophisticated local data changes tracking and when snapshot listener fires after I changed something on the very same device, this sophisticated system fails. I'd like to filter out local changes - that were done on the same device - and not get any updates from snapshot listener unless the changes come from somewhere else.

I'm writing an iOS app using Swift if that matters but I believe the principles should be the same for all platforms

1 Answers

This should be straightforward by using includeMetadataChanges in your listen call and then observing the isFromCache property of the snapshot metadata.

If isFromCache is true, that means the change is being propagated locally and is not guaranteed to be server-correct. If isFromCache is false, that means it is the acknowledged server value. Since you will be including metadata-only changes in the listen, it will fire even if the server ack is just confirming the already-sent local data.

Related