Ios 在核心数据更改处理期间捕获异常,尝试使用userInfo(null)插入nil
我在应用程序中使用coreData来保存多个表(大约30个),它工作正常,但随机崩溃,出现以下异常:Ios 在核心数据更改处理期间捕获异常,尝试使用userInfo(null)插入nil,ios,swift,exception,core-data,runtime-error,Ios,Swift,Exception,Core Data,Runtime Error,我在应用程序中使用coreData来保存多个表(大约30个),它工作正常,但随机崩溃,出现以下异常: [error] error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification
[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-06-16 15:05:56.043490+0500 Sales CRM[619:85792]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
(0x1872c6fe0 0x185d28538 0x1872c6f28 0x1871e316c 0x1895c2028 0x1895c1218 0x1895c97d0 0x1895c019c 0x1001d6888 0x1001bb78c 0x1000a43d8 0x1878d034c 0x1878e8048 0x187d95814 0x187cda770 0x187ccab28 0x187d97bb0 0x100f85a10 0x100f932e8 0x100f89634 0x100f95630 0x100f9539c 0x186387100 0x186386cac)
libc++abi.dylib: terminating with uncaught exception of type NSException
我搜索了很多,但大多数解决方案都在目标C中,甚至我也遵循了一些解决方案,但没有一个有效
这是我的密码:
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 as well
class func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
上面是我的上下文,每当我从数据库中保存或获取任何东西时,都会调用它。下面是我在coreData中保存的方式:
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "DoctorsDetail_Tbl", in: context)
let newDoc = NSManagedObject(entity: entity!, insertInto: context)
newDoc.setValue(empid, forKey: "empId")
newDoc.setValue(doctorid, forKey: "docId")
//save the object
do {
try context.save()
print("saved Doctors 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)")
}
}
有时您会在数据源
(empid,doctorid)
中获得nil
值,您希望将其插入coredata
在将值插入数据库之前,请选中nil
func test() {
for (key, value) in self {
if let unwrappedValue = value.asOptional {
print("Unwrapped value for '\(key)' is '\(unwrappedValue)'")
} else {
print("Value for '\(key)' is nil")
}
}
}
用途:
在数组中过滤nil:
extension Array {
static func filterNils(array: [T?]) -> [T] {
return array.filter { $0 != nil }.map { $0! }
}
}
用途:
希望这能有所帮助。我知道了,但它在大多数情况下都工作得很好,只是偶尔会随机崩溃。@shahtajkhalid,明白你的意思了,所以我建议你在插入之前每次在数组或dict中检查nil值。如果你找到了解决方案,请标记为正确。。为了更好地指导其他so用户。@vaibhav解决方案没有起作用,还是有同样的问题!
extension Array {
static func filterNils(array: [T?]) -> [T] {
return array.filter { $0 != nil }.map { $0! }
}
}
var array:[Int?] = [1, nil, 2, 3, nil]
Array.filterNils(array)