RestKit 数据映射导致应用程序崩溃

Posted

技术标签:

【中文标题】RestKit 数据映射导致应用程序崩溃【英文标题】:RestKit Data Mapping causing application to crash 【发布时间】:2013-12-11 09:40:37 【问题描述】:

我需要使用 RestKit 映射一个灵活的数据结构,但我没有运气。

我有一个 Activity 资源,其中包含一组 Extra 子资源。 每个 Extra 资源都包含一个 key 和一个 value。当然,由于最后这些不是真正的资源,因此拥有 ID 没有任何意义。

这里有一个Activity资源的例子:

  
            "_id": "xxxxx",
            ...
            "extras": [
                
                    "key": "group",
                    "value": "xxxx"
                ,
                
                    "key": "name",
                    "value": "xxxxx"
                ,
                
                    "key": "asd",
                    "value": "xxx"
                
            ]
  

这是我用于映射的代码:

- (RKEntityMapping *)activityMapping 
RKEntityMapping * activityMapping = [RKEntityMapping mappingForEntityForName:@"SSActivity"
                                                        inManagedObjectStore:self.managedObjectStore];
[activityMapping addAttributeMappingsFromDictionary:@
                                                      @"_id"         : @"activityId"
                                                      ];

[activityMapping addRelationshipMappingWithSourceKeyPath:@"extras" mapping:[self activityExtraMapping]];

activityMapping.identificationAttributes = @[@"activityId"];

return activityMapping;


- (RKEntityMapping *)activityExtraMapping 
   RKEntityMapping * activityExtraMapping = [RKEntityMapping mappingForEntityForName:@"SSActivityExtra"
                                                        inManagedObjectStore:self.managedObjectStore];
[activityExtraMapping addAttributeMappingsFromDictionary:@
                                                           @"key" : @"key",
                                                           @"value" : @"value"
                                                           ];
return activityExtraMapping;

SSActivityExtra 类只包含 keyvalue 字符串属性,而 SSActivity 包含 extra 属性。

@property (nonatomic, retain) NSSet * extras;

现在,只要 restkit 映射请求,我就会返回以下异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM _stateFlags]: unrecognized selector sent to instance 0xadc4560'
*** First throw call stack:
(
0   CoreFoundation                      0x0358b5e4 __exceptionPreprocess + 180  
1   libobjc.A.dylib                     0x0330e8b6 objc_exception_throw + 44  
2   CoreFoundation                      0x03628903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0357b90b ___forwarding___ + 1019
4   CoreFoundation                      0x0357b4ee _CF_forwarding_prep_0 + 14
5   CoreData                            0x00bad03e -[NSRelationshipDescription(_NSInternalMethods) _nonPredicateValidateValue:forKey:inObject:error:] + 270
6   CoreData                            0x00babdeb -[NSManagedObject(_NSInternalMethods) _validateValue:forProperty:andKey:withIndex:error:] + 379
7   CoreData                            0x00c3b408 -[NSManagedObject validateValue:forKey:error:] + 136
8   Foundation                          0x013f12c0 -[NSObject(NSKeyValueCoding) validateValue:forKeyPath:error:] + 435
9   SeeApp                              0x00207cee -[RKMappingOperation validateValue:atKeyPath:] + 398
10  SeeApp                              0x00208434 -[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:] + 1108
11  SeeApp                              0x0020dd77 -[RKMappingOperation mapOneToManyRelationshipWithValue:mapping:] + 2503
12  SeeApp                              0x002101cf -[RKMappingOperation applyRelationshipMappings] + 6815
13  SeeApp                              0x00212540 -[RKMappingOperation main] + 4224
14  Foundation                          0x01405829 -[__NSOperationInternal _start:] + 671
15  Foundation                          0x01382558 -[NSOperation start] + 83
16  SeeApp                              0x001ffb25 -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] + 1957
17  SeeApp                              0x001ff14c __63-[RKMapperOperation mapRepresentations:atKeyPath:usingMapping:]_block_invoke + 460
18  CoreFoundation                      0x035855eb __NSArrayEnumerate + 571
19  CoreFoundation                      0x03585196 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 102
20  CoreFoundation                      0x035850a5 -[NSArray enumerateObjectsUsingBlock:] + 53
21  SeeApp                              0x001fedfc -[RKMapperOperation mapRepresentations:atKeyPath:usingMapping:] + 2364
22  SeeApp                              0x00200c10 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 560
23  SeeApp                              0x00201672 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 2210
24  SeeApp                              0x0020203b -[RKMapperOperation main] + 1403
25  Foundation                          0x01405829 -[__NSOperationInternal _start:] + 671
26  Foundation                          0x01382558 -[NSOperation start] + 83
27  SeeApp                              0x002542a7 __73-[RKManagedObjectResponseMapperOperation performMappingWithObject:error:]_block_invoke + 4887
28  CoreData                            0x00bccfef developerSubmittedBlockToNSManagedObjectContextPerform + 95
29  libdispatch.dylib                   0x045474b0 _dispatch_client_callout + 14
30  libdispatch.dylib                   0x04534778 _dispatch_barrier_sync_f_invoke + 58
31  libdispatch.dylib                   0x04534422 dispatch_barrier_sync_f + 89
32  CoreData                            0x00bccf1f -[NSManagedObjectContext performBlockAndWait:] + 127
33  SeeApp                              0x00252a91 -[RKManagedObjectResponseMapperOperation performMappingWithObject:error:] + 993
34  SeeApp                              0x00250413 -[RKResponseMapperOperation main] + 2371
35  Foundation                          0x01405829 -[__NSOperationInternal _start:] + 671
36  Foundation                          0x01382558 -[NSOperation start] + 83
37  Foundation                          0x01407af4 __NSOQSchedule_f + 62
38  libdispatch.dylib                   0x045474b0 _dispatch_client_callout + 14
39  libdispatch.dylib                   0x0453507f _dispatch_queue_drain + 452
40  libdispatch.dylib                   0x04534e7a _dispatch_queue_invoke + 128
41  libdispatch.dylib                   0x04535e1f _dispatch_root_queue_drain + 83
42  libdispatch.dylib                   0x04536137 _dispatch_worker_thread2 + 39
43  libsystem_pthread.dylib             0x048d3dab _pthread_wqthread + 336
44  libsystem_pthread.dylib             0x048d7cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我知道这可能类似于 this,但我认为不是,建议的解决方案没有帮助。

对这里发生的事情有任何想法吗?我已经为此苦苦挣扎了 2 天。

编辑 12-12:添加模型截图

SSActivityExtra 数据模型

SSActivity 数据模型

【问题讨论】:

打开跟踪日志并添加一个所有异常断点以尝试获取有关异常位置的更多信息。此外,您能否添加数据模型的图片。你们有什么验证方法吗? 不,我没有任何数据模型。另外,我按要求添加了屏幕截图。 【参考方案1】:

我能够通过这种方式解决类似的问题:我没有在 Core Data 中配置一对多关系。 配置后这个问题就解决了。

【讨论】:

【参考方案2】:

不确定这是否会导致异常,但这段代码是错误的:

[activityExtraMapping addAttributeMappingsFromDictionary:@
                                                       @"key" : @"key",
                                                       @"value" : @"value"
                                                       ];

应该是这样的:

[activityExtraMapping addAttributeMappingsFromDictionary:@
                                                       @"key" : @"extraKey",
                                                       @"value" : @"extraValue"
                                                       ];

异常可能是由于 extraKeyextraValue 属性是非可选的,因此当 RestKit 尝试保存模型时会引发异常...

【讨论】:

以上是关于RestKit 数据映射导致应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

使用 RestKit 导致核心数据崩溃的多个 RKResponseDescriptor

将输出映射到 Restkit 中的核心数据时崩溃

RestKit 0.20.1 将本地 JSON 映射到 Core Data 崩溃

RestKit 对象映射在 BOOL 的空 JSON 值上崩溃

当 Restkit 尝试映射时,带有字符串字段(包含 JSON 字符串)的 Json 崩溃

Restkit 对象映射两个类错误访问