         if managedObjectContext.hasChanges {
             do {
                 try managedObjectContext.save()
-            } catch {
+            }
+            catch {
                 fatalError("Error migrating store: \(error)")
             }
+        }
+    }
+    
+    // DataController.write {...} closure execute data updates before context save.
+    static func write(closure: ()->void) {
+        dispatch_async(CoreDataWriteQueue) {
+            closure()
+            DataController.saveContext()
         }
     }
     

            return managedObjectContext!
        }
        
        let coordinator: NSPersistentStoreCoordinator? = self.persistentStoreCoordinator
        if coordinator != nil {
            managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
            managedObjectContext?.persistentStoreCoordinator = coordinator!
            managedObjectContext?.undoManager = nil
            managedObjectContext?.mergePolicy = NSErrorMergePolicy
            

import UIKit
import CoreData

let CoreDataWriteQueue: dispatch_queue_t = dispatch_queue_create("BraveDataWriteQueue", DISPATCH_QUEUE_SERIAL)

class DataController: NSObject {
    static let singleton = DataController()
    
    private var _managedObjectContext: NSManagedObjectContext? = nil
    var managedObjectContext: NSManagedObjectContext? {
        get {
            if NSThread.isMainThread() {
                return mainThreadContext()
            }
            else {
                return writeContext()
            }
        }
