1

我正在从 Web 服务中获取裸 JSON 数组中的一些 URL,如下所示:

[
    "https://server1.com",
    "https://server2.com"
]

我想将这些映射到单独的核心数据实体,但遇到断言失败:

I restkit.network:RKObjectRequestOperation.m:180 GET 'https://server-test.com/Client/Endpoints'
*** Assertion failure in NSDictionary *RKEntityIdentificationAttributesForEntityMappingWithRepresentation(RKEntityMapping *__strong, NSDictionary *__strong)(), /Users/jonukas/MyApp/Pods/RestKit/Code/CoreData/RKManagedObjectMappingOperationDataSource.m:83
An uncaught exception was raised
Expected a dictionary representation

FWIW,设置断点RKManagedObjectMappingOperationDataSource.m:83表明应该是 a 的NSDictionary表示实际上是一个RKMappingSourceObject.

这是我制作的映射:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"MyURL"
                                               inManagedObjectStore:self.manager.managedObjectStore];

[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil
                                                                  toKeyPath:@"url"]];

RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                method:RKRequestMethodGET
                                                                           pathPattern:@"Client/Endpoints"
                                                                               keyPath:nil
                                                                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.manager addResponseDescriptor:descriptor];

我的实体只有一个可转换类型的属性 (url)。这是NSManagedObject子类:

@interface MyURL : NSManagedObject

@property (nonatomic, retain) id url;

@end

我的映射是否不正确,或者我的实体描述/对象模型有问题(请原谅,我刚开始使用 Core Data)?

更新:我尝试设置一个标识属性,但似乎没有帮助:

[mapping setIdentificationAttributes:@[@"url"]];

打开跟踪日志以获取RestKit/ObjectMapping结果:

D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: (
    "https://server1.com",
    "https://server2.com"
)
 and targetObject: (null)
T restkit.object_mapping:RKMapperOperation.m:320 Examining keyPath '<null>' for mappable content...
D restkit.object_mapping:RKMapperOperation.m:297 Found mappable collection at keyPath '<null>': (
    "https://server1.com",
    "https://server2.com"
)
*** Assertion failure in NSDictionary *RKEntityIdentificationAttributesForEntityMappingWithRepresentation(RKEntityMapping *__strong, NSDictionary *__strong)(), /Users/jonukas/MyApp/Pods/RestKit/Code/CoreData/RKManagedObjectMappingOperationDataSource.m:83
…

我猜(空)targetObject 可能是我的问题。

更新 2: RestKit 0.21.0 解决了这个问题。

4

1 回答 1

2

url将您的核心数据模型中的属性更改为String类型。通过将其设置为transformableRestKit 无法正确解释应该是什么数据类型,因此它选择了字典并且变得相当混乱。

我假设您将其设置为可转换的,因为您想要一个NSURL. RestKit 无法为您做到这一点,因此您应该使用瞬态属性来实现该目标。

于 2013-09-30T20:24:48.323 回答