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
}