使用RestKit将裸露的URL数组映射到核心数据实体

时间:2013-09-30 20:13:58

标签: core-data restkit

我从一个像以下网站服务的裸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上设置断点表明应该是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
…

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

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

1 个答案:

答案 0 :(得分:2)

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

我假设您将其设置为可转换,因为您需要NSURL。 RestKit不能为你做到这一点,所以你应该使用一个瞬态属性来实现那个监狱。