我经常搜索这个错误,但无法找到解决方案。
我有我的托管对象:
lazy var managedObjectContext: NSManagedObjectContext = {
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
我有一个Singleton类
lazy var managedObjectContext : NSManagedObjectContext? = {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
return appDelegate.managedObjectContext
}
return nil
}()
每当我从数据库
更新/删除/加载数据时,我都会有一个被称为大量时间的方法class func LoadDatabase(tableName: String, predicateString: String, predicateIntValue: Int) -> [AnyObject]? {
do {
let managedObjectContext = Singleton.sharedInstance.managedObjectContext
if managedObjectContext == nil {
return nil
}
let fReq: NSFetchRequest = NSFetchRequest(entityName: tableName)
if predicateString != "" {
fReq.predicate = NSPredicate(format: predicateString, predicateIntValue)
}
var result: [AnyObject]
do {
if managedObjectContext == nil {
return nil
}
result = try managedObjectContext!.executeFetchRequest(fReq)
} catch {
return nil
}
// update exist event
if
result.count != 0 {
return result
}
} catch let error1 as NSError {
print("No values to load for table: \(error1)")
}
return nil
}
我认为我在并发访问方面存在问题。当应用程序启动时,它会通过异步请求来更新数据库。但我也从varius控制器调用LoadDatabase方法。一旦异步调用完成了更新datable,它就会调用另一种方法来删除远程数据库中不再存在的数据。
我尝试更改为MainQueueConcurrency,但现在我也遇到了这个错误:
2016-03-20 21:27:01.371 Tamil League[1070:245784] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)
2016-03-20 21:27:01.371 Tamil League[1070:245784] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
(0x24a9e2eb 0x2426adff 0x24a9e231 0x249e7fe5 0x261eb683 0x261ea063 0x261e2dfb 0x24a60b21 0x24a5ee17 0x24a5f255 0x249b1bb9 0x249b19ad 0x25c2baf9 0x28c9dfb5 0xa6d5c 0x24664873)
libc++abi.dylib: terminating with uncaught exception of type NSException
有谁知道如何解决这个问题?