0

Getting to the point -

  • Have a chat application that was working fine.
  • When a user sends a message, I convert the local timestamp to GMT timestamp to maintain international sync and i store GMT in firebase server.
  • The listener listens for new message, receives this message, converts it back to local timestamp and refreshes the table view.

Everything was working flawlessly until recently I noticed a funny issue

  • I was chatting between a simulator and a real device
  • When the real device receives a chat from the simulator, I quickly respond say in like a second, the chat is sent and everything is sorted the right way according to message timestamp
  • But when the simulator receives a chat from the real device and I quickly respond in a second from the simulator, the chat is inserted before the previous chat message instead of inserting after the previous chat message

I noticed that my code and everything was fine. The problem was that when I use the current timestamp Date() on my simulator, it seems to be lagging by a good 11 - 15 seconds (and this varies sometimes even 30 seconds) from using Date() on a real device. So consequently when I immediately send a message from simulator, the logged timestamp on the firebase server lags by an average 20 seconds and since my messages are sorting according to timestamp, it puts this message before the last received message when really it should come after.

Is this a known simulator issue? I can't test it on another real device cuz I have just one

func sendChatMessage() {
       let localDate = Date()
       let GMTTimestamp = getGMTTimestamp(localDate: localDate.timeintervalsince1970)
       let GMTTimestampFinal = Int64(GMTTimestamp)

       //perform server write operation
}
Archid
  • 377
  • 6
  • 21
  • 2
    If your app is timestamp sensitive, why are you using a date generated by the device in the first place? That timestamp could be way off - if the the device hasn't update to local time wherever it is, or they have that feature turned off or maybe they just changed the time for some other reason. Why not use the Firebase servers timestamp? – Jay Jun 20 '20 at 13:01
  • @Jay Hi Jay. That's what I was thinking as well. So the firebase timestamp would always record the timestamp of the firebase server is it? How do I go about handling international dates and stuff? And plus my app is fully coded to use timeintervalsince1970. Will look into it but would appreciate any suggestions – Archid Jun 20 '20 at 13:56
  • 1
    Hey Archid. The Firebase server timestamp is indeed determined by the server, and is in a fixed timezone. You'll typically want to take that value and display it in the user's timezone in your application code. – Frank van Puffelen Jun 20 '20 at 14:39
  • @FrankvanPuffelen - thanks frank - I was thinking about server timestamps but it completely skipped my mind that devices are unstable. Will do this. Either you or Jay can post this as an answer and I'll mark it as the correct answer. This is the appropriate approach I guess – Archid Jun 20 '20 at 14:54
  • I'll leave that to Jay, since he came up with the idea. :) – Frank van Puffelen Jun 20 '20 at 14:58
  • It is for sure the appropriate approach. Implementing Firebase timestamps can be a bit tricky to start with but fortunately there are lot of posts here on SO about how to do that so be sure to research and read the other posts. – Jay Jun 20 '20 at 15:01

1 Answers1

1

I think best practice here is that if your app is timestamp sensitive, to use a fixed and stable timestamp source which would be using Firebase Timestamp.

Locally generated dates or timestamps can be unstable and unpredictable; if the device hasn't updated to local time wherever it is, or they have that feature was turned off or maybe they just changed the time for some other reason. That can lead to inaccuracies in your data and sorting sequence.

The Real Time Database Timestamp and Timestamp has a different implementation than the Cloud Firestore one but there are a number of posts and examples here for implementing a server based timestamp.

Jay
  • 34,438
  • 18
  • 52
  • 81