核心数据子/父保存异常

时间:2013-06-10 13:17:33

标签: macos cocoa core-data

我崩溃了(可能是在父母moc保存的时候:

这就是moc创建的方式(父级):

   if (_managedObjectContextInMemoryForDynamicInformation) return _managedObjectContextInMemoryForDynamicInformation;
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinatorInMemoryForDynamicInformation];
    if (!coordinator) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
        [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
        NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
    _managedObjectContextInMemoryForDynamicInformation = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContextInMemoryForDynamicInformation setPersistentStoreCoordinator:coordinator];
    [_managedObjectContextInMemoryForDynamicInformation setUndoManager:nil];
    return _managedObjectContextInMemoryForDynamicInformation;

子:

    AppDelegate *delegateMain = (AppDelegate *)[[NSApplication sharedApplication] delegate];
    _mocInMemoryForDynamicInformation = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [_mocInMemoryForDynamicInformation setUndoManager:nil];
    _mocInMemoryForDynamicInformation.parentContext = delegateMain.managedObjectContextInMemoryForDynamicInformation;

这是我如何保存:

-(void) finalSaveMocInMemoryForDynamicInformation;
{
    [_mocInMemoryForDynamicInformation performBlock:^{
        // do something that takes some time asynchronously using the temp context

        // push to parent
        NSError *error;
        if ([_mocInMemoryForDynamicInformation save:&error])
        {
            AppDelegate *delegateMain = (AppDelegate *)[[NSApplication sharedApplication] delegate];

            // save parent to disk asynchronously
            [delegateMain.managedObjectContextInMemoryForDynamicInformation performBlock:^{
                @try {
                    NSError *error;
                    if (![delegateMain.managedObjectContextInMemoryForDynamicInformation save:&error])
                    {
                        [self logError:error];
                    }
                }
                @catch (NSException *exception) {
                    NSLog(@"ERROR:%@:%@ exception->%@ inside managedObjectContextInMemoryForDynamicInformation",[self class], NSStringFromSelector(_cmd),exception);
                    //[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:moc];

                    return ;
                }

            }];
        } else [self logError:error];
    }];
}
  

特定应用信息:    * 由于未捕获的异常'NSObjectInaccessibleException'而终止应用程序,原因:'CoreData无法满足   '0x7fd8a71d87a0的错误   “”   abort()调用terminate,称为抛出异常

     

应用程序特定的Backtrace 1:0 CoreFoundation
  0x00007fff86e6f0a6 __exceptionPreprocess + 198 1 libobjc.A.dylib
  0x00007fff849353f0 objc_exception_throw + 43 2 CoreData
  0x00007fff8737eec4 _PFFaultHandlerLookupRow + 1348 3 CoreData
  0x00007fff8737e61f _PF_FulfillDeferredFault + 239 4 CoreData
  0x00007fff873892cd _PF_ManagedObject_WillChangeValueForKeyIndex + 77 5   CoreData 0x00007fff8738cc51   _sharedIMPL_setvfk_core + 129 6 CoreData 0x00007fff873a4eaa - [NSManagedObject(_NSInternalMethods)   _didChangeValue:forRelationship:named:withInverse:] + 1434 7 Foundation 0x00007fff878fe860   NSKeyValueNotifyObserver + 390 8基金会
  0x00007fff87900279 NSKeyValueDidChange + 456 9基金会   0x00007fff878bb7df - [NSObject(NSKeyValueObserverNotification)   didChangeValueForKey:] + 130 10 CoreData
  0x00007fff873898c1 - [NSManagedObject didChangeValueForKey:] + 113 11   CoreData 0x00007fff873e566d    - [NSManagedObject(_NSInternalMethods)_updateFromRefreshSnapshot:includingTransients:] + 685 12 CoreData 0x00007fff873b0969 - [NSManagedObjectContext(_NestedContextSupport)   _copyChildObject:toParentObject:fromChildContext:] + 121 13 CoreData 0x00007fff873b0505 - [NSManagedObjectContext(_NestedContextSupport)   _parentProcessSaveRequest:inContext:error:] + 629 14 CoreData 0x00007fff873df72a __82- [NSManagedObjectContext(_NestedContextSupport)   executeRequest:withContext:error:] _ block_invoke_0 + 634 15   libdispatch.dylib 0x00007fff8aad6a2d   _dispatch_barrier_sync_f_slow_invoke + 77 16 libdispatch.dylib 0x00007fff8aad10b6 _dispatch_client_callout + 8 17 libdispatch.dylib   0x00007fff8aad60c8 _dispatch_main_queue_callback_4CF + 275 18   CoreFoundation 0x00007fff86e110fe __CFRunLoopRun   + 1614 19 CoreFoundation 0x00007fff86e106b2 CFRunLoopRunSpecific + 290 20 HIToolbox
  0x00007fff89d950a4 RunCurrentEventLoopInMode + 209 21 HIToolbox
  0x00007fff89d94e42 ReceiveNextEventCommon + 356 22 HIToolbox
  0x00007fff89d94cd3 BlockUntilNextEventMatchingListInMode + 62 23   AppKit 0x00007fff8ffcd613 _DPSNextEvent +   685 24 AppKit 0x00007fff8ffcced2    - [NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128 25 AppKit 0x00007fff8ffc4283    - [NSApplication run] + 517 26 AppKit 0x00007fff8ff68cb6 NSApplicationMain + 869 27 callsfreecall
  0x000000010e6ab472 main + 34 28 libdyld.dylib
  0x00007fff858a47e1 start + 0 29 ???   0x0000000000000002 0x0 + 2

2 个答案:

答案 0 :(得分:1)

您的var标识符确实令人困惑。像parentContext,子Context,rootContext等名称是不是很棘手?

无论如何,问题可能不是保存操作。在第一个视图中,您看起来更改了对象与另一个对象的关系,但另一个对象不再存在。如果另一个对象出现在子上下文中并在商店中删除,则会发生这种情况。 (可能是因为父上下文删除了对象并保存了它的状态。)

答案 1 :(得分:1)

现在,我找到了崩溃的解决方案。

该代码中传递了崩溃:

    [_mocInMemoryForDynamicInformation obtainPermanentIDsForObjects:@[session] error:&error];

这是苹果中的已知错误,我无法获得永久ID。如果我尝试在子上下文中执行此操作,则save是崩溃的产品。

我就是这样解决的:

    [_mocInMemoryForDynamicInformation.parentContext obtainPermanentIDsForObjects:@[session] error:&error];

这个简单的解决方案解决了所有问题(我无法解释这是如何工作的。)