我收到很多带有以下错误的崩溃报告。到目前为止,它只发生在运行 iOS7 的设备上。
Fatal Exception NSInternalInconsistencyException
这是 JSON 解析操作的一般代码。记录导致错误的行。
- (void)main {
@autoreleasepool {
self.objectContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
self.objectContext.parentContext = [[CoreDataController sharedController] managedObjectContext];
[self.objectContext setRetainsRegisteredObjects:YES];
[self.objectContext performBlock:^{
id jsonObject = [self.rawData objectFromJSONData];
@try {
[self parseJsonObject:jsonObject];
}
@catch (NSException *exception) {
NSLog(@"Caught Exception: %@", exception);
}
[self parsingFinished];
}];
}
}
- (void)parsingFinished {
if ([self.parsingResults count] ==0){
[self parsingFailed:[NSError errorWithDomain:@"Parsing or Loading Error"
code:100
userInfo:[NSDictionary dictionaryWithObject:@"Parsing finished with no data."
forKey:@"error"]]];
return;
}
@try {
NSError *saveError = nil;
if (![self.objectContext save:&saveError])
NSLog(@"saveError %@", saveError);
}
@catch (NSException *exception) {
NSLog(@"Caught Exception: %@", exception);
}
[[CoreDataController sharedController] saveContext]; //ERROR
if (self.parsingCompletionBlock)
self.parsingCompletionBlock(self.parsingResults);
}
核心数据控制器方法在这里:
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
[managedObjectContext performBlockAndWait:^{
NSError *error = nil;
//THE FOLLOWING LINE CAUSES THE ERROR
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}];
}
}
我认为这与多线程核心数据有关,但我迷路了。上面的代码中是否有可见的修复?
编辑:这里有更多的崩溃日志。
Fatal Exception NSInternalInconsistencyException
This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.
com.apple.main-thread Crashed
0 ... libsystem_kernel.dylib __pthread_kill + 8
1 libsystem_pthread.dylib pthread_kill + 58
2 libsystem_c.dylib abort + 76
3 libc++abi.dylib abort_message + 74
4 libc++abi.dylib default_terminate_handler() + 252
5 libobjc.A.dylib _objc_terminate() + 192
6 libc++abi.dylib std::__terminate(void (*)()) + 78
7 libc++abi.dylib __cxa_increment_exception_refcount
8 libobjc.A.dylib objc_exception_rethrow + 42
9 CoreData -[NSManagedObjectContext save:] + 1494
10 My App
CoreDataController.m line 64
__33-[CoreDataController saveContext]_block_invoke
11 CoreData developerSubmittedBlockToNSManagedObjectContextPerform + 88
12 ... libdispatch.dylib _dispatch_barrier_sync_f_slow_invoke + 66
13 libdispatch.dylib _dispatch_client_callout + 22
14 libdispatch.dylib _dispatch_main_queue_callback_4CF$VARIANT$mp + 268
15 CoreFoundation __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
16 CoreFoundation __CFRunLoopRun + 1300
17 CoreFoundation CFRunLoopRunSpecific + 522
18 CoreFoundation CFRunLoopRunInMode + 106
19 GraphicsServices GSEventRunModal + 138
20 UIKit UIApplicationMain + 1136