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

时间:2013-12-11 09:40:37

标签: objective-c ios7 restkit restkit-0.20

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

我有一个活动资源,其中包含一系列额外子资源。 每个额外资源都包含密钥。当然,由于最后这些不是真正的资源,因此对他们来说没有任何意义。

此处有一个活动资源的示例:

  {
            "_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 类只包含字符串属性,而 SSActivity 包含 extra < / em> property。

@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类似,但我认为不是,建议的解决方案没有帮助。

这里发生了什么?我已经在这方面苦苦挣扎了两天。

编辑12-12:添加了模型屏幕截图

SSActivityExtra数据模型

SSActivityExtra data model

SSActivity数据模型

SSActivity data model

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

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

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

应该是:

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

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