I noticed that the downloaded timestamps of objects that are created by FIRServerValue.timestamp()
are slightly different than the online version. My code:
let messagesRef = self.ref.child("messages").child(chatId)
messagesRef
.queryOrderedByChild("timestamp")
.queryLimitedToLast(500)
.observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
guard let object = snapshot.value as? [String: AnyObject] else { return }
let messageId = snapshot.key
if let
senderId = object["senderId"] as? String,
senderName = object["senderName"] as? String,
senderEmail = object["senderEmail"] as? String,
timestamp = object["timestamp"] as? NSTimeInterval {
let date = timestamp.toDate()
let text = object["text"] as? String
print("text: \(text) - timestamp: \(timestamp)")
}
})
Here is a sample output compared to the online value (marked by ->
):
text: Optional("1") - timestamp: 1471596374007.0 -> 1471596374874
text: Optional("2") - timestamp: 1471596375044.0 -> 1471596375324
text: Optional("3") - timestamp: 1471596376157.0 -> 1471596376461
text: Optional("4") - timestamp: 1471596461213.0 -> 1471596463220
text: Optional("5") - timestamp: 1471596542659.0 -> 1471596543307
I sometimes encounter a bug where a messages comes before another message, even though it has been sent after that particular message. I assume it has something to do with this behaviour. When the retrieved timestamps are not exact, messages that are sent close to each other in time, can be ordered differently.