Weird, but kind of expected issue with Firebase Timestamp. Value event listener triggered twice!

267 views
Skip to first unread message

Tobi Adewuyi

unread,
Mar 28, 2016, 1:43:19 PM3/28/16
to Firebase Google Group
Hi
Just started learning Swift, and spent the last 3 days tearing my hair out because I couldn't figure out why my Value event listener was being called twice. I was trying to attach a timestamp to an object I was persisting in Firebase, using the function:
FirebaseServerValue.timestamp()
After saving my object, I noticed that my Value event listener was being triggered twice. Speaking to you as a bald man, I figured out that the added timestamp was what was causing the issue. Not entirely sure why this happens? Maybe the object is persisted to the database first, and then the timestamp added immediately after, causing the Value event listener to trigger twice? Don't know. If anyone has an explanation for this, that'll be great!

Chris Raynor

unread,
Mar 28, 2016, 2:07:14 PM3/28/16
to Firebase Google Group
This is part of the 'latency compensation' in the clients that allow Firebase apps to continue to feel responsive even if they lose network connectivity - if there are listeners out on the device that made the change, they will first be fired with a best-guess timestamp calculated from the local time and a server time offset we established last time you connected to the server. Once the write has gone through (which could be hours later, depending on connectivity and usage), the same listener will fire again with the actual timestamp inserted by the server. In conclusion:
  • This only happens on the device making the write - all others will only be updated with the server-written timestamp
  • This only happens to listeners on the location
  • The callback on the write only fires when the server has acknowledged it, so will only see the server-written timestamp
In general the number of times a listener calls shouldn't really be a concern - the state of the data at the location is what's being sync'ed and not the specific order of changes to that data; the server might combine updates that have happened while you were offline into a single update, so you should re-render the whole item instead of trying to work out what changed.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/4df13bb7-c504-4149-90d8-20aec120419c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages