CoreData无法解决未修改关系的错误

时间:2014-01-20 07:15:47

标签: ios core-data

我有一个实体A,它有B的关系。

这是从CoreData获取的数组:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"isSynchronized = NO"];
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];
NSArray * newWordsToSynchronize = [A MR_findAllWithPredicate:predicate inContext:context];
NSMutableArray * newWordsParameters = [NSMutableArray arrayWithCapacity:[asToSynchronize count]];
[newWordsToSynchronize enumerateObjectsUsingBlock:^(A * a, NSUInteger idx, BOOL * stop) {
    NSLog(@"============= may have some problem ============");
    NSLog(@"[a hasFaultForRelationshipNamed:@\"b\"] = %d", [a hasFaultForRelationshipNamed:@"b"]);
    NSLog(@"a.word = %@", a.word);
    NSLog(@"a.b.objectID = %@", a.b.objectID);
    NSLog(@"a.managedObjectContext = %@", a.managedObjectContext);
    NSLog(@"a.b.managedObjectContext = %@", [a.b managedObjectContext]);
    NSLog(@"a.b.managedObjectContext = %@", [a.b managedObjectContext]);
    NSLog(@"a.managedObjectContext = %@", a.managedObjectContext);
    NSDictionary *paramDictionary = @{
            @"nbook" : a.b.name,
            @"lang" : a.lang,
            ...
    };
    [newWordsParameters addObject:paramDictionary];
}];

当我第二次调用它时,[a.b managedObjectContext]方法可能返回nil,然后应用程序崩溃。

来自控制台的崩溃日志:

2014-01-20 14:29:14.561 MyApp[10563:2e07] a.b.objectID = 0x1754e940 <x-coredata://DFC4F956-D5AC-4264-8717-C07090CA9547/B/p1>
2014-01-20 14:29:14.561 MyApp[10563:2e07] a.managedObjectContext = <NSManagedObjectContext: 0x1754bc00>
2014-01-20 14:29:14.561 MyApp[10563:2e07] a.b.managedObjectContext = <NSManagedObjectContext: 0x1754bc00>
2014-01-20 14:29:14.562 MyApp[10563:2e07] a.b.managedObjectContext = (null)
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x1754e940 <x-coredata://DFC4F956-D5AC-4264-8717-C07090CA9547/B/p1>''
*** First throw call stack:
(
    0   CoreFoundation                      0x037495e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x034cc8b6 objc_exception_throw + 44
    2   CoreData                            0x031a233b _PFFaultHandlerLookupRow + 2715
    3   CoreData                            0x031a1897 -[NSFaultHandler fulfillFault:withContext:forIndex:] + 39
    4   CoreData                            0x031a1473 _PF_FulfillDeferredFault + 259
    5   CoreData                            0x031a12c6 _sharedIMPL_pvfk_core + 70
    6   CoreData                            0x031e7130 _pvfk_5 + 32
    7   MyApp                        0x00181b00 __68-[CichangAHTTPEngine synchronizeAsOnCompletion:failure:]_block_invoke + 1760
    8   CoreFoundation                      0x037435eb __NSArrayEnumerate + 571
    9   CoreFoundation                      0x03743196 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
    10  CoreFoundation                      0x037430a5 -[NSArray enumerateObjectsUsingBlock:] + 53
    11  MyApp                        0x0018105b -[CichangAHTTPEngine synchronizeAsOnCompletion:failure:] + 1179
    12  MyApp                        0x00185d9e __65-[CichangAHTTPEngine downloader:didFinishedDownloadObject:]_block_invoke_3 + 2030
    13  libdispatch.dylib                   0x040717f8 _dispatch_call_block_and_release + 15
    14  libdispatch.dylib                   0x040864b0 _dispatch_client_callout + 14
    15  libdispatch.dylib                   0x04074eeb _dispatch_root_queue_drain + 287
    16  libdispatch.dylib                   0x04075137 _dispatch_worker_thread2 + 39
    17  libsystem_pthread.dylib             0x04412dab _pthread_wqthread + 336
    18  libsystem_pthread.dylib             0x04416cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type _NSCoreDataException

我发现在B首次呼叫后,[a.b managedObjectContext]将被取消分配。

我认为这主要是由Core Data 1550错误造成的(如果我不调用[a.b managedObjectContext],Core Data会报告“操作无法完成。(Cocoa error 1550.)”)。但是什么可能导致1550错误以及如何修复它?

1550错误报告:

Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn’t be completed. (Cocoa error 1550.)" UserInfo=0x167269c0 {NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1550.), Dangling reference to an invalid object.=null, NSValidationErrorObject=<A: 0x16bab420> (entity: A; id: 0x16baae50 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/A/p168> ; data: {
    date = "2014-01-21 02:32:00 +0000";
    expect = "2014-01-21 02:31:59 +0000";
    isSynchronized = 1;
    lang = en;
    last = "2014-01-21 02:01:59 +0000";
    level = 1;
    b = "0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1>";
    status = "-1";
    studycount = 0;
    trans = "v. \U6d4b\U91cf\Uff0c\U6743\U8861 ";
    word = measure;
}), NSAffectedObjectsErrorKey=(
    "<B: 0x16babd70> (entity: B; id: 0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1> ; data: <fault>)"
), NSValidationErrorKey=newWordBook, NSValidationErrorValue=<B: 0x16babd70> (entity: B; id: 0x16bab460 <x-coredata://A85EE9AB-97C1-43C1-B92E-A6C906F0C1A8/B/p1> ; data: <fault>), NSValidationErrorShouldAttemptRecoveryKey=true}

如果1550错误发生,“CoreData无法完成故障”崩溃将更容易造成。

1 个答案:

答案 0 :(得分:2)

“核心数据无法实现错误”实际上意味着删除了。持久性商店中有数据。然后创建表示此数据的托管对象。对象是一个错误。然后删除了商店中的数据。然后访问了一个对象的一些属性,Core Data试图解决这个问题,但是没有。

如果您没有进行显式删除,那么还要确保不会发生隐式删除。当您的一个关系配置了级联删除规则时,可能会发生幕后删除。

在您的示例中,如果使用级联规则配置从A到B的关系,并且删除了对象A,则可能会发生这种情况。