当我在一个子上下文中更改托管对象(之后保存)并在其他子上下文中删除(首先保存)时,我的应用程序崩溃了。
如何重现:
1.用空应用程序创建新项目'启用模板和核心数据
2.将managedObjectContext getter更改为以下(我已更改并发类型)
- (NSManagedObjectContext *)managedObjectContext { if (_managedObjectContext != nil) { return _managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:coordinator]; } return _managedObjectContext; }
3.请将didfinishLaunching方法替换为以下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; { //insert NSManagedObjectContext *insertingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [insertingContext setParentContext:self.managedObjectContext]; [insertingContext performBlockAndWait:^{ Test *test = (Test *)[NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:insertingContext]; test.test=@"test"; [insertingContext save:nil]; [self.managedObjectContext performBlockAndWait:^{ [self.managedObjectContext save:nil]; }]; NSLog(@"inserted and saved to persistance store"); }]; } { //get the mo and change the property NSManagedObjectContext *acceesingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; { [acceesingContext setParentContext:self.managedObjectContext]; [acceesingContext performBlockAndWait:^{ NSFetchRequest *request = [[NSFetchRequest alloc] init] ; [request setEntity:[NSEntityDescription entityForName:@"Test" inManagedObjectContext:acceesingContext]]; NSArray *results = [acceesingContext executeFetchRequest:request error:nil]; if ([results count] > 0 ) { Test *test= [results objectAtIndex:0]; test.test=@"Hello"; NSLog(@"accessed and changed the property so that fault is fired"); } }]; } { NSManagedObjectContext *deletingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [deletingContext setParentContext:self.managedObjectContext]; [deletingContext performBlockAndWait:^{ NSFetchRequest *request = [[NSFetchRequest alloc] init] ; [request setEntity:[NSEntityDescription entityForName:@"Test" inManagedObjectContext:deletingContext]]; NSArray *results = [deletingContext executeFetchRequest:request error:nil]; if ([results count] > 0 ) { Test *test= [results objectAtIndex:0]; [deletingContext deleteObject:test]; [deletingContext save:nil]; [self.managedObjectContext performBlockAndWait:^{ [self.managedObjectContext save:nil]; }]; NSLog(@"deleted and saved to persistance store"); } }]; } [acceesingContext performBlock:^{ // it is crashing here, please help. [acceesingContext save:nil]; }]; } return YES; }
4.最后添加名为' Test'的实体具有属性' test' (NSString)并运行应用程序
我遇到的问题是,当一个子moc已经获取托管对象并更改了其中的属性时,其他子项删除并将更改保存到持久性存储。保存已修改mo的moc时发生崩溃。
崩溃报告
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xd0000000001c0000 '' *** First throw call stack: ( 0 CoreFoundation 0x0000000101bf0795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000101953991 objc_exception_throw + 43 2 CoreData 0x0000000100278a93 _PFFaultHandlerLookupRow + 1075 3 CoreData 0x00000001003063a3 -[NSManagedObject(_NSInternalMethods) _updateFromRefreshSnapshot:includingTransients:] + 243 4 CoreData 0x00000001002aa563 -[NSManagedObjectContext(_NestedContextSupport) _copyChildObject:toParentObject:fromChildContext:] + 771 5 CoreData 0x00000001002aa01b -[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 1019 6 CoreData 0x0000000100310243 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 563 7 libdispatch.dylib 0x0000000101fc205a _dispatch_barrier_sync_f_slow_invoke + 45 8 libdispatch.dylib 0x0000000101fd16fd _dispatch_client_callout + 8 9 libdispatch.dylib 0x0000000101fc146c _dispatch_main_queue_callback_4CF + 354 10 CoreFoundation 0x0000000101c4e729 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 11 CoreFoundation 0x0000000101b9b9a4 __CFRunLoopRun + 1764 12 CoreFoundation 0x0000000101b9aed3 CFRunLoopRunSpecific + 467 13 GraphicsServices 0x0000000103b893a4 GSEventRunModal + 161 14 UIKit 0x00000001005bba63 UIApplicationMain + 1010 15 TestCrash 0x00000001000040a3 main + 115 16 libdyld.dylib 0x000000010227e7e1 start + 0 17 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException
答案 0 :(得分:9)
这里的关键不是堆栈跟踪中的那一行,而是异常消息:
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xd0000000001c0000 ''
如果Core Data抱怨对象不可访问,则意味着它无法在持久性存储中找到该对象。我们来看看代码中出现这种情况的原因:
insertingContext
创建实例并在父级中保存更改。此时,您的一个对象位于持久性存储文件中。acceesingContext
获取此实例并对其进行更改,但您不保存更改。此时,acceesingContext
对刚刚提取的对象进行了内存中未保存的更改。deletingContext
获取对象并将其删除。然后保存deletingContext
和父级中的更改。此时,您已从持久性存储文件中删除了该对象。 然而,这一点至关重要,acceesingContext
仍有未保存的对象更改。acceesingContext
上保存更改。由于acceesingContext
对您提取的对象进行了未保存的更改,因此会尝试更新该对象。但是该对象在持久性存储中不存在,因为您删除了它。由于acceesingContext
无法更新不存在的对象,因此会引发异常并且应用程序崩溃。使用嵌套托管对象上下文时要记住的一件事是,保存更改只会将更改推向一个方向 - 朝向父级。如果您有两个兄弟情境(在这种情况下为acceesingContext
和deletingContext
),则保存对一个孩子的更改不会自动更新另一个孩子。
由于这显然是演示代码(感谢BTW,让它更容易理解!)很难完全确定您需要在真实应用代码中做些什么。一种典型的方法是监听NSManagedObjectContextDidSaveNotification
,然后使用mergeChangesFromContextDidSaveNotification:
将更改从一个上下文应用到另一个上下文。这样,当一个上下文删除对象时,您可以更新其他上下文以反映该事实。这使得可以使多个托管对象上下文保持彼此同步。