4

我正在为我的应用程序使用 restkit 0.10.2。我有不同的表,其中数据从服务器获取并存储到数据库中。

我有三张表,一张主表和两张子表。两个子表都与主表具有一对一的关系。

在此处输入图像描述

我有两个不同的视图控制器 V1 和 V2,我在其中调用 web 服务并将数据映射到表 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"];

从 DB 中删除 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

谢谢

4

2 回答 2

0

似乎问题在于您正在尝试刷新已删除的对象。问题导致这段代码:

for (Child1 *object in objects) {
        [context refreshObject:object mergeChanges:NO];
    }
[context save:&saveError];
NIDPRINT(@"All records are deleted in Nearby- %@", saveError);
于 2013-12-25T09:03:03.640 回答
0

我认为问题是你删除对象并在同一个线程上刷新它。该对象在尝试刷新此对象时已被删除。在尝试刷新之前确保删除对象已完成。

于 2014-02-06T09:35:27.290 回答