CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常

时间:2013-11-19 04:40:43

标签: ios objective-c core-data

嗨,我正在崩溃,

当我试图在背景中向数据库插入1000条记录时,我遇到以下异常: CoreData: error: Serious application error。在核心数据更改处理期间发现了异常 这通常是

观察者中的错误
NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)2013-11-19 09:41:19.587 3pTalk[7487:907]

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'

我使用代码插入objexts

dispatch_queue_t myBackgroundQ = dispatch_queue_create("com.sample.addressbook", NULL);
// Could also get a global queue; in this case, don't release it below.
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
    [self userAddressbook];
});
dispatch_release(myBackgroundQ);
[self performSelectorOnMainThread:@selector(startSyncLoader) withObject:nil waitUntilDone:YES];

2 个答案:

答案 0 :(得分:16)

不要同时使用一个上下文从2个线程(Main,Background)访问同一个数据库(持久存储协调器)。不建议这样做。它导致App Crash。

创建NSManagedContext对象并设置persistentstoreCoordinator。

    dispatch_queue_t request_queue = dispatch_queue_create("com.xxx.ScsMethod", NULL);
    dispatch_async(request_queue, ^{
    NSPersistentStoreCoordinator *mainThreadContextStoreCoordinator = [context     persistentStoreCoordinator]; //
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; // 
    [context setPersistentStoreCoordinator:mainThreadContextStoreCoordinator];}

答案 1 :(得分:2)

您是否同时从两个线程访问数据库?使用相同的上下文?这可能是原因。看到这个问题

Problems Adding to NSMutableArray: attempt to insert nil object at 10