diff --git a/Yep/Services/FayeService.swift b/Yep/Services/FayeService.swift index e987f361..163d69e4 100644 --- a/Yep/Services/FayeService.swift +++ b/Yep/Services/FayeService.swift @@ -250,7 +250,7 @@ class FayeService: NSObject, MZFayeClientDelegate { } } - syncMessageWithMessageInfo(messageInfo, inRealm: realm) { messageIDs in + syncMessageWithMessageInfo(messageInfo, messageAge: .New, inRealm: realm) { messageIDs in tryPostNewMessagesReceivedNotificationWithMessageIDs(messageIDs, withMessageAge: .New) } } diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 5a946111..8f2e6448 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1619,6 +1619,15 @@ enum TimeDirection { case Future(minMessageID: String) case Past(maxMessageID: String) case None + + var messageAge: MessageAge { + switch self { + case .Past: + return .Old + default: + return .New + } + } } func messagesFromRecipient(recipient: Recipient, withTimeDirection timeDirection: TimeDirection, failureHandler: ((Reason, String?) -> Void)?, completion: Bool -> Void) { @@ -1654,7 +1663,7 @@ func messagesFromRecipient(recipient: Recipient, withTimeDirection timeDirection var messageIDs = [String]() for messageInfo in unreadMessagesData { - syncMessageWithMessageInfo(messageInfo, inRealm: realm) { _messageIDs in + syncMessageWithMessageInfo(messageInfo, messageAge: timeDirection.messageAge, inRealm: realm) { _messageIDs in messageIDs += _messageIDs } } diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index d51dbc5a..690d95cb 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -735,7 +735,7 @@ func syncUnreadMessagesAndDoFurtherAction(furtherAction: (messageIDs: [String]) var messageIDs = [String]() for messageInfo in allUnreadMessages { - syncMessageWithMessageInfo(messageInfo, inRealm: realm) { _messageIDs in + syncMessageWithMessageInfo(messageInfo, messageAge: .New, inRealm: realm) { _messageIDs in messageIDs += _messageIDs } } @@ -859,7 +859,7 @@ func recordMessageWithMessageID(messageID: String, detailInfo messageInfo: JSOND } } -func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Realm, andDoFurtherAction furtherAction: ((messageIDs: [String]) -> Void)? ) { +func syncMessageWithMessageInfo(messageInfo: JSONDictionary, messageAge: MessageAge, inRealm realm: Realm, andDoFurtherAction furtherAction: ((messageIDs: [String]) -> Void)? ) { func deleteMessage(message: Message, inRealm realm: Realm) { realm.write { @@ -879,15 +879,16 @@ func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Real newMessage.createdUnixTime = updatedUnixTime } - /* - // 确保网络来的消息比任何已有的消息都要新,防止服务器消息延后发来导致插入到当前消息上面 - if let latestMessage = realm.objects(Message).sorted("createdUnixTime", ascending: true).last { - if newMessage.createdUnixTime < latestMessage.createdUnixTime { - println("before newMessage.createdUnixTime: \(newMessage.createdUnixTime)") - newMessage.createdUnixTime = latestMessage.createdUnixTime + YepConfig.Message.localNewerTimeInterval - println("adjust newMessage.createdUnixTime: \(newMessage.createdUnixTime)") + if case .New = messageAge { + // 确保网络来的新消息比任何已有的消息都要新,防止服务器消息延后发来导致插入到当前消息上面 + if let latestMessage = realm.objects(Message).sorted("createdUnixTime", ascending: true).last { + if newMessage.createdUnixTime < latestMessage.createdUnixTime { + println("before newMessage.createdUnixTime: \(newMessage.createdUnixTime)") + newMessage.createdUnixTime = latestMessage.createdUnixTime + YepConfig.Message.localNewerTimeInterval + println("adjust newMessage.createdUnixTime: \(newMessage.createdUnixTime)") + } } - }*/ + } realm.write { realm.add(newMessage)