我被这个运行时错误困住了,这个错误在刷新数据或启动(从服务获取并在coreData中保存)时发生了50%的时间。在这个问题上,我已经看过了几乎所有的解决方案,但是大多数都是在Obj中,但是我对iOS还是新手,并且使用的很迅速。我有大约20张桌子,它们都使用相同的上下文。
下面是我的代码:
public class ServiceCalls : NSManagedObject {
/*
class func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let moc = NSManagedObjectContext(concurrencyType:.mainQueueConcurrencyType)
let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateMOC.parent = moc
privateMOC.perform({
do {
try privateMOC.save()
} catch {
fatalError("Failure to save context: \(error)")
}
})
return appDelegate.persistentContainer.viewContext
}
*/ // tried this but didn't work
class func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
这就是我使用它的方式,下面只是在数据库中保存数据的一个例子。
class func SaveCustomerContacts(name : String,id : String){
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "AllCustomerContacts_Tbl", in: context)
let newDoc = NSManagedObject(entity: entity!, insertInto: context)
newDoc.setValue(name, forKey: "contactName")
newDoc.setValue(id, forKey: "id")
//save the object
do {
try context.save()
print("saved Customer contacts in Database yayy!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
这是正在引发的完全异常:
[error] 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)
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)
2017-07-31 16:09:20.908 Sales CRM[5846:202190] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
发布于 2017-08-01 06:57:52
此错误指示正在插入某些nil
值。
[error] 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)
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)
2017-07-31 16:09:20.908 Sales CRM[5846:202190] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
这一问题可能有若干原因:
MainQueueConcurrencyType
的上下文。使用PrivateQueueConcurrencyType
代替。nil
或某种错误数据(在关系中)。https://stackoverflow.com/questions/45414169
复制相似问题