         }
     }
 
-    private func markMessageAsReaded(message: Message) {
-
-        if message.readed {
-            return
-        }
-
-        // 防止未在此界面时被标记
-
-        if navigationController?.topViewController == self {
-
-            let messageID = message.messageID
-
-            dispatch_async(realmQueue) {
-                guard let realm = try? Realm() else {
-                    return
-                }
-                
-                if let message = messageWithMessageID(messageID, inRealm: realm) {
-                    let _ = try? realm.write {
-                        message.readed = true
-                    }
-
-                    markAsReadMessage(message, failureHandler: nil) { success in
-                        if success {
-                            println("appear Mark message \(messageID) as read")
-                        }
-                    }
-                }
-            }
+//    private func markMessageAsReaded(message: Message) {
+//
+//        if message.readed {
+//            return
+//        }
+//
+//        // 防止未在此界面时被标记
+//
+//        if navigationController?.topViewController == self {
+//
+//            let messageID = message.messageID
+//
+//            dispatch_async(realmQueue) {
+//                guard let realm = try? Realm() else {
+//                    return
+//                }
+//                
+//                if let message = messageWithMessageID(messageID, inRealm: realm) {
+//                    let _ = try? realm.write {
+//                        message.readed = true
+//                    }
+//
+//                    markAsReadMessage(message, failureHandler: nil) { success in
+//                        if success {
+//                            println("appear Mark message \(messageID) as read")
+//                        }
+//                    }
+//                }
+//            }
+//        }
+//    }
+
+    private func batchMarkMessagesAsReaded(needUpdateAllMessages needUpdateAllMessages: Bool = false) {
+
+        if let _ = conversation.withFriend, recipient = conversation.recipient, latestMessage = messages.last {
+
+            if needUpdateAllMessages {
+                messages.forEach { message in
+                    if !message.readed {
+                        let _ = try? realm.write {
+                            message.readed = true
+                        }
+                    }
+                }
+
+            } else {
+                let _ = try? realm.write {
+                    latestMessage.readed = true
+                }
+            }
+
+            batchMarkAsReadOfMessagesToRecipient(recipient, beforeMessage: latestMessage, failureHandler: nil, completion: {
+                println("batchMarkAsReadOfMessagesToRecipient OK")
+            })
         }
     }
 


        // 进来时就尽快标记已读

        conversation.messages.filter({ message in
            if let fromFriend = message.fromFriend {
                
                return (message.readed == false) && (fromFriend.friendState != UserFriendState.Me.rawValue)
            } else {
                return false
            }
        }).forEach({ self.markMessageAsReaded($0) })

        // MARK: Notify Typing



                if sender.friendState != UserFriendState.Me.rawValue { // from Friend

                    if !message.readed {
                        batchMarkMessagesAsReaded()
                    }

                    switch message.mediaType {

