使用Restkit进行iOS对象映射时,应用程序意外崩溃。

4
我正在使用restkit 0.10.2来开发我的应用程序。我有不同的表格,从服务器中获取数据并存储到数据库中。我有三个表格,一个主表和两个子表格。两个子表格都与主表格有一对一的关系。
我有两个不同的视图控制器V1和V2,在这些控制器中,我调用webservice并将数据映射到Child1和Child2表格中。Child1、Child2和Master表格都有userID作为主键。
当我点击ViewController V1/V2时,以下过程正在执行。
1) Getting Data from Server.
2) Delete UserID from Child1/Child2 table(if any), not deleting details in Master Table. Delete Rule is nullify.
3) Map userID to Child1/Child2 Table and Other Details(name, age, userId etc) with Master Table.

映射代码:

RKManagedObjectMapping *userDetailMapping = [RKManagedObjectMapping mappingForClass:[Master class] inManagedObjectStore:manager.objectStore];
        [userDetailMapping mapKeyPath:@"uid" toAttribute:@"userID"];
        [userDetailMapping mapKeyPath:@"ne" toAttribute:@"name"];
        [userDetailMapping mapKeyPath:@"email" toAttribute:@"email"];
[manager.mappingProvider setMapping:userDetailMapping forKeyPath:@"users"];

        userDetailMapping.primaryKeyAttribute = @"userID";


RKManagedObjectMapping *userkeyMapping1 = [RKManagedObjectMapping mappingForClass:[Child1 class] inManagedObjectStore:manager.objectStore];
        [userkeyMapping1 mapKeyPath:@"uk" toAttribute:@"userID"];
        userkeyMapping1.primaryKeyAttribute = @"userID";
        [manager.mappingProvider setMapping:userkeyMapping1 forKeyPath:@"usk1"];

 RKManagedObjectMapping *userkeyMapping2 = [RKManagedObjectMapping mappingForClass:[Child2 class] inManagedObjectStore:manager.objectStore];
        [userkeyMapping2 mapKeyPath:@"uk" toAttribute:@"userID"];
        userkeyMapping2.primaryKeyAttribute = @"userID";
        [manager.mappingProvider setMapping:userkeyMapping2 forKeyPath:@"usk2"];

连接关系

[userkeyMapping1 hasOne:@"userDetail" withMapping:userDetailMapping];
        [userkeyMapping1 connectRelationship:@"userDetail" withObjectForPrimaryKeyAttribute:@"userID"];
        [userDetailMapping hasOne:@"userDetail1" withMapping:userkeyMapping1];
        [userDetailMapping connectRelationship:@"userDetail1" withObjectForPrimaryKeyAttribute:@"userID"];


[userkeyMapping2 hasOne:@"userDetail" withMapping:userDetailMapping];
        [userkeyMapping2 connectRelationship:@"userDetail" withObjectForPrimaryKeyAttribute:@"userID"];
        [userDetailMapping hasOne:@"userDetail2" withMapping:userkeyMapping2];
        [userDetailMapping connectRelationship:@"userDetail2" withObjectForPrimaryKeyAttribute:@"userID"];

从数据库中删除Child1和Child2的对象

- (void)clearUserKeyTable {
    RKObjectManager *manager = [[RestKit sharedDataManager] objectManager];

    NSManagedObjectContext *context = [Child1 currentContext];

        NSArray *objects = [Child1 findAll];
        for (Child1 *object in objects) {
            [context deleteObject:object];
        }

        NSError *saveError = nil;
        [context save:&saveError];
        NIDPRINT(@"All records are deleted - %@", saveError);
        for (Child1 *object in objects) {
            [context refreshObject:object mergeChanges:NO];
        }
        [context save:&saveError];
        NIDPRINT(@"All records are deleted in Nearby- %@", saveError);
}

注意:视图1将在3分钟内自动刷新。因此,获取和映射操作将执行,无论屏幕如何。
问题:有时当我手动刷新VC1页面时,它会崩溃。我不知道确切的原因。在我的模拟器中,它显示为:
'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x8a5b410 <x-coredata:/XXXX-XXXX-XXXX/Child1/p128>.

当我使用我的设备进行调试时,它抛出了一个异常,并且这是一个崩溃日志。

Path:            /var/mobile/Applications/25FD9991-8134-476D-AADE-66D95BFC3C5B/MyApp.app/MyApp
Identifier:      MyApp
Version:         ??? (???)
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2013-02-25 23:17:27.645 -0600
OS Version:      iOS 6.0.1 (10A523)
Report Version:  104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Crashed Thread:  10

Last Exception Backtrace:
0   CoreFoundation                  0x3147329e __exceptionPreprocess + 158
1   libobjc.A.dylib                 0x3233197a objc_exception_throw + 26
2   CoreData                        0x32741b38 _PFFaultHandlerLookupRow + 1472
3   CoreData                        0x327430fa _PF_FulfillDeferredFault + 198
4   CoreData                        0x3274ac0c _PF_ManagedObject_WillChangeValueForKeyIndex + 64
5   CoreData                        0x3275532e _sharedIMPL_setvfk_core + 106
6   CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
7   CoreData                        0x32750a4c -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 328
8   CoreData                        0x32751216 -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:] + 1286
9   CoreData                        0x3275a82e -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:] + 318
10  Foundation                      0x32fd4eac NSKeyValueNotifyObserver + 268
11  Foundation                      0x32fd4b06 NSKeyValueDidChange + 330
12  Foundation                      0x32faeec6 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 90
13  CoreData                        0x3274ae26 _PF_ManagedObject_DidChangeValueForKeyIndex + 98
14  CoreData                        0x327553a2 _sharedIMPL_setvfk_core + 222
15  CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
16  CoreData                        0x32750ad6 -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 466
17  CoreData                        0x32751216 -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:] + 1286
18  CoreData                        0x3275a82e -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:] + 318
19  Foundation                      0x32fd4eac NSKeyValueNotifyObserver + 268
20  Foundation                      0x32fd4b06 NSKeyValueDidChange + 330
21  Foundation                      0x32faeec6 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 90
22  CoreData                        0x3274ae26 _PF_ManagedObject_DidChangeValueForKeyIndex + 98
23  CoreData                        0x327553a2 _sharedIMPL_setvfk_core + 222
24  CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
25  CoreData                        0x3274bd1e -[NSManagedObject setValue:forKey:] + 86
26  Foundation                      0x32faf71e -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 294
27  MyApp                       0x0019aa5c -[RKManagedObjectMappingOperation connectRelationship:] (RKManagedObjectMappingOperation.m:100)
28  MyApp                       0x0019af16 __55-[RKManagedObjectMappingOperation connectRelationships]_block_invoke_0 (RKManagedObjectMappingOperation.m:115)
29  Foundation                      0x32fdc534 -[NSBlockOperation main] + 196
30  Foundation                      0x32fd3db4 -[__NSOperationInternal start] + 836
31  MyApp                       0x001b063c -[RKMappingOperationQueue waitUntilAllOperationsAreFinished] (RKMappingOperationQueue.m:53)
32  MyApp                       0x001b6496 -[RKObjectMapper performMapping] (RKObjectMapper.m:368)
33  MyApp                       0x001b14f0 -[RKObjectLoader mapResponseWithMappingProvider:toObject:inContext:error:] (RKObjectLoader.m:227)
34  MyApp                       0x001b18c2 -[RKObjectLoader performMapping:] (RKObjectLoader.m:271)
35  MyApp                       0x001b1a9e __47-[RKObjectLoader performMappingInDispatchQueue]_block_invoke_0 (RKObjectLoader.m:282)
36  libdispatch.dylib               0x33a6311a _dispatch_call_block_and_release + 6
37  libdispatch.dylib               0x33a62996 _dispatch_queue_drain$VARIANT$up + 142
38  libdispatch.dylib               0x33a62890 _dispatch_queue_invoke$VARIANT$up + 32
39  libdispatch.dylib               0x33a71210 _dispatch_root_queue_drain + 188
40  libdispatch.dylib               0x33a713b4 _dispatch_worker_thread2 + 80
41  libsystem_c.dylib               0x33aa1a0c _pthread_wqthread + 356
42  libsystem_c.dylib               0x33aa18a0 start_wqthread + 4

线程10崩溃了

Thread 10 name:  Dispatch queue: org.restkit.ObjectMapping
Thread 10 Crashed:
0   libsystem_kernel.dylib          0x30661350 __pthread_kill + 8
1   libsystem_c.dylib               0x33aca11e pthread_kill + 54
2   libsystem_c.dylib               0x33b0696e abort + 90
3   libc++abi.dylib                 0x314e7d4a abort_message + 70
4   libc++abi.dylib                 0x314e4ff4 _ZL17default_terminatev + 20
5   libobjc.A.dylib                 0x32331a74 _ZL15_objc_terminatev + 144
6   libc++abi.dylib                 0x314e5078 _ZL19safe_handler_callerPFvvE + 76
7   libc++abi.dylib                 0x314e5110 std::terminate() + 16
8   libc++abi.dylib                 0x314e6594 __cxa_rethrow + 84
9   libobjc.A.dylib                 0x323319cc objc_exception_rethrow + 8
10  CoreData                        0x32741f60 _PFFaultHandlerLookupRow + 2536
11  CoreData                        0x327430fa _PF_FulfillDeferredFault + 198
12  CoreData                        0x3274ac0c _PF_ManagedObject_WillChangeValueForKeyIndex + 64
13  CoreData                        0x3275532e _sharedIMPL_setvfk_core + 106
14  CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
15  CoreData                        0x32750a4c -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 328
16  CoreData                        0x32751216 -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:] + 1286
17  CoreData                        0x3275a82e -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:] + 318
18  Foundation                      0x32fd4eac NSKeyValueNotifyObserver + 268
19  Foundation                      0x32fd4b06 NSKeyValueDidChange + 330
20  Foundation                      0x32faeec6 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 90
21  CoreData                        0x3274ae28 _PF_ManagedObject_DidChangeValueForKeyIndex + 100
22  CoreData                        0x327553a2 _sharedIMPL_setvfk_core + 222
23  CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
24  CoreData                        0x32750ad6 -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 466
25  CoreData                        0x32751216 -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:] + 1286
26  CoreData                        0x3275a82e -[NSManagedObjectContext observeValueForKeyPath:ofObject:change:context:] + 318
27  Foundation                      0x32fd4eac NSKeyValueNotifyObserver + 268
28  Foundation                      0x32fd4b06 NSKeyValueDidChange + 330
29  Foundation                      0x32faeec6 -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 90
30  CoreData                        0x3274ae28 _PF_ManagedObject_DidChangeValueForKeyIndex + 100
31  CoreData                        0x327553a2 _sharedIMPL_setvfk_core + 222
32  CoreData                        0x327497c2 _PF_Handler_Public_SetProperty + 54
33  CoreData                        0x3274bd1e -[NSManagedObject setValue:forKey:] + 86
34  Foundation                      0x32faf71e -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 294
35  MyApp                       0x0019aa5c -[RKManagedObjectMappingOperation connectRelationship:] (RKManagedObjectMappingOperation.m:100)
36  MyApp                       0x0019af16 __55-[RKManagedObjectMappingOperation connectRelationships]_block_invoke_0 (RKManagedObjectMappingOperation.m:115)
37  Foundation                      0x32fdc536 -[NSBlockOperation main] + 198
38  Foundation                      0x32fd3db4 -[__NSOperationInternal start] + 836
39  MyApp                       0x001b063c -[RKMappingOperationQueue waitUntilAllOperationsAreFinished] (RKMappingOperationQueue.m:53)
40  MyApp                       0x001b6496 -[RKObjectMapper performMapping] (RKObjectMapper.m:368)
41  MyApp                       0x001b14f0 -[RKObjectLoader mapResponseWithMappingProvider:toObject:inContext:error:] (RKObjectLoader.m:227)
42  MyApp                       0x001b18c2 -[RKObjectLoader performMapping:] (RKObjectLoader.m:271)
43  MyApp                       0x001b1a9e __47-[RKObjectLoader performMappingInDispatchQueue]_block_invoke_0 (RKObjectLoader.m:282)
44  libdispatch.dylib               0x33a6311c _dispatch_call_block_and_release + 8
45  libdispatch.dylib               0x33a62996 _dispatch_queue_drain$VARIANT$up + 142
46  libdispatch.dylib               0x33a62890 _dispatch_queue_invoke$VARIANT$up + 32
47  libdispatch.dylib               0x33a71212 _dispatch_root_queue_drain + 190
48  libdispatch.dylib               0x33a713b4 _dispatch_worker_thread2 + 80
49  libsystem_c.dylib               0x33aa1a0e _pthread_wqthread + 358
50  libsystem_c.dylib               0x33aa18a0 start_wqthread + 4

谢谢


你是否从多个线程访问核心数据?你必须非常小心,因为它不会自动工作...关于这个问题有很多S.O.的问题/答案。 - nielsbot
我正在使用同一线程访问和删除对象。 - iPhone Guy
我发现应用程序在clearUserKeyTable方法上崩溃了。但我不知道确切的问题所在。 - iPhone Guy
2个回答

0

看起来问题在于您试图刷新已删除的对象。问题出现在以下代码段:

for (Child1 *object in objects) {
        [context refreshObject:object mergeChanges:NO];
    }
[context save:&saveError];
NIDPRINT(@"All records are deleted in Nearby- %@", saveError);

0
我认为问题在于您在同一线程上删除对象并刷新它。当它尝试刷新该对象时,该对象已被删除。请确保在尝试刷新之前已经完成了删除对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接