当我尝试使用CoreData插入一系列数据时,它随机崩溃,并显示以下错误消息:
2019-04-03 17:24:31.217510+0530 YOUR APP[8862:401750] [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)
2019-04-03 17:24:31.223609+0530 YOUR APP[8862:401750] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107c371bb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x00000001071dd735 objc_exception_throw + 48
2 CoreFoundation 0x0000000107c37015 +[NSException raise:format:] + 197
3 CoreFoundation 0x0000000107c128da -[__NSCFSet addObject:] + 202
4 CoreData 0x00000001068cbb2b -[NSManagedObjectContext(_NSInternalChangeProcessing) _processPendingUpdates:] + 379
5 CoreData 0x00000001068c632b -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1163
6 CoreData 0x000000010689ee8a _performRunLoopAction + 394
7 CoreFoundation 0x0000000107b9bc27 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
8 CoreFoundation 0x0000000107b960be __CFRunLoopDoObservers + 430
9 CoreFoundation 0x0000000107b96751 __CFRunLoopRun + 1537
10 CoreFoundation 0x0000000107b95e11 CFRunLoopRunSpecific + 625
11 GraphicsServices 0x000000011160a1dd GSEventRunModal + 62
12 UIKitCore 0x000000010c90a81d UIApplicationMain + 140
13 YOUR APP 0x0000000104e23031 main + 225
14 libdyld.dylib 0x000000010b419575 start + 1
15 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我无法弄清楚崩溃背后的原因,但是此崩溃发生在核心数据中“插入/检索”发生的1/3倍。
请在下面找到代码
public func insertUser(userList: [user], completion : (Bool) -> ()){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
do {
let userEntity = NSEntityDescription.entity(forEntityName: "User_Table", in: context)
for user in userList {
let userBrandName = NSManagedObject(entity: userEntity!, insertInto: context)
userBrandName.setValue(id, forKey: "Identity")
userBrandName.setValue(name, forKey: "Name")
.....
userBrandName.setValue(discount, forKey: "Discount")
try context.save()
}
completion(true)
} catch {
completion(false)
}
}
我无法找到其背后的问题/原因。我是Swift和Core Data的新手