     
     func insertOrUpdateOneEntity<T: NSManagedObject>(entityName: String, predicate: NSPredicate, itemHandler: ((item: T) -> Void)) -> Bool {
         
-        if let existItem: T = fetchOneEntity(entityName, predicate: predicate) {
-            itemHandler(item: existItem)
-            return saveContext()
-        }
-        else {
-            guard let newItem = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: managedObjectContext) as? T
-                else { return false }
-            
-            itemHandler(item: newItem)
-            
-            return saveContext()
-        }
+        let moc = currentManagedObjectContext
+        
+        moc.performBlockAndWait { 
+            if let existItem: T = self.fetchOneEntity(entityName, predicate: predicate) {
+                itemHandler(item: existItem)
+            }
+            else {
+                guard let newItem = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: moc) as? T
+                    else { return }
+                
+                itemHandler(item: newItem)
+            }
+        }
+        
+        return saveThreadContext(moc)
     }
     
     func saveContext () -> Bool {

        
        let fetchRequest = fetchForEntity(entityName, predicate: predicate, sort: sort, pageSize: pageSize, pageIndex: pageIndex)
        
        do {
            let items = try managedObjectContext.executeFetchRequest(fetchRequest) as? [T]
            return items
        }
        catch let error as NSError {
            Log.error?.message("fetch fail, \(error.localizedDescription)")
        }
        
        return nil
    }
    
    func fetchOneEntity<T: NSManagedObject>(entityName: String,

        }
    }
    
    func saveThreadContext(moc: NSManagedObjectContext) -> Bool {
        var result = false
        
        moc.mergePolicy = mergePolicy
        
        
        moc.performBlockAndWait {
            if !moc.hasChanges {
                return
            }
            
            do {
                try moc.save()
                result = true
                dispatch_async(dispatch_get_main_queue(), {
                    self.saveContext()
                })
            }
            catch {
                let nserror = error as NSError
                NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
        
        return result
    }
    
    func saveThreadContext(context: NSManagedObjectContext, completion: ((error: NSError?)->Void)?) {
        let managedObjectContext = context
        managedObjectContext.mergePolicy = mergePolicy
