 
                         dispatch_async(dispatch_get_main_queue()) {
 
-                            if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
-                                updateThumbnailOfMessage(message, withThumbnailFileName: fileName)
+                            let realm = Realm()
+                            
+                            if let message = messageWithMessageID(messageID, inRealm: realm) {
+                                if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
+                                    updateThumbnailOfMessage(message, withThumbnailFileName: fileName, inRealm: realm)
+                                }
                             }
                         }
                     }


func downloadAttachmentOfMessage(message: Message) {

    func updateAttachmentOfMessage(message: Message, withAttachmentFileName attachmentFileName: String) {
        if let realm = message.realm {
            realm.beginWrite()
            message.localAttachmentName = attachmentFileName
            message.downloadState = MessageDownloadState.Downloaded.rawValue
            realm.commitWrite()
        }
    }

    func updateThumbnailOfMessage(message: Message, withThumbnailFileName thumbnailFileName: String) {
        if let realm = message.realm {
            realm.beginWrite()
            message.localThumbnailName = thumbnailFileName
            realm.commitWrite()
        }
    }

    let attachmentURLString = message.attachmentURLString

    if !attachmentURLString.isEmpty && message.downloadState != MessageDownloadState.Downloaded.rawValue {
        if let url = NSURL(string: attachmentURLString) {

                    let fileName = NSUUID().UUIDString

                    dispatch_async(dispatch_get_main_queue()) {
                        let realm = Realm()
                        
                        if let message = messageWithMessageID(messageID, inRealm: realm) {
                            
                            switch mediaType {
                                
                            case MessageMediaType.Image.rawValue:
                                if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
                                    updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
                                }
                                
                            case MessageMediaType.Video.rawValue:
                                if let fileURL = NSFileManager.saveMessageVideoData(data, withName: fileName) {
                                    updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
                                }
                                
                            case MessageMediaType.Audio.rawValue:
                                if let fileURL = NSFileManager.saveMessageAudioData(data, withName: fileName) {
                                    updateAttachmentOfMessage(message, withAttachmentFileName: fileName, inRealm: realm)
                                }
                                
                            default:
                                break
                            }
                        }
                    }
                }
            }
        }
    }

    if mediaType == MessageMediaType.Video.rawValue {
        let thumbnailURLString = message.thumbnailURLString

        if !thumbnailURLString.isEmpty && message.localThumbnailName.isEmpty {
            
            if let url = NSURL(string: thumbnailURLString) {
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                    
                    let data = NSData(contentsOfURL: url)

                    if let data = data {
