我正在从 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 解决了这个问题。