         
         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
+        let moc = currentManagedObjectContext
+        
+        var items : [T]? = nil
+        
+        moc.performBlockAndWait { 
+            do {
+                items = try moc.executeFetchRequest(fetchRequest) as? [T]
+            }
+            catch let error as NSError {
+                Log.error?.message("fetch fail, \(error.localizedDescription)")
+            }
+        }
+
+        return items
     }
     
     func fetchOneEntity<T: NSManagedObject>(entityName: String,

    func removeAllEntity(entityName: String, predicate: NSPredicate? = nil) -> Bool {
        guard let items = fetchEntity(entityName, predicate: predicate) else { return true }
        
        for item in items {
            managedObjectContext.deleteObject(item)
        }
        
        return saveContext()
    }
    
    func insertOrUpdateOneEntity<T: NSManagedObject>(entityName: String,

    
    func countForEntity(entityName: String, predicate: NSPredicate? = nil) -> Int? {
        
        var result: Int? = nil
        managedObjectContext.performBlockAndWait { 
            var error: NSError? = nil
            
            result = self.managedObjectContext.countForFetchRequest(self.countForEntity(entityName, predicate: predicate), error: &error)
            if let error = error {
                Log.error?.message("fetch count fail, \(error.localizedDescription)")
            }
        }
        
        return result
