我正在尝试从以下 JSON 映射到一个 CoreData 属性:
{
"products": [
{
"stock": {
"inStock": true,
"inStockBeforeMaxAdvanceOrderingDate": false
},
"url": "xxxxxxxxxxx",
"manufacturer": "Demo",
"images": [
{
"format": "thumbnail",
"imageType": "PRIMARY",
"url": "/imageURL/xx.jpg"
},
{
"format": "thumbnailGrid",
"imageType": "PRIMARY",
"url": "/imageURL/xxx.jpg"
}
],
"productFees": [
[
"EMPTY",
"0.0"
]
],
"name": "demo product"
}
]
}
我正在尝试仅从图像集合中提取 thumbnailGrid 图像 url。在我的回复中,我有很多次返回一个集合,但我只对一个领域感兴趣。我可以在 CoreData 中设置关系并且我没有问题,但是因为我知道我只会匹配一个资源,所以我更愿意存储在一个属性中而不是关系中。
我的产品 CoreData 实体有一个 thumbnailImageURL 属性。
我的产品实体映射:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:_managedObjectStore];
[mapping addAttributeMappingsFromDictionary:@{
@"stock.inStock":@"inStock",
@"stock.inStockBeforeMaxAdvanceOrderingDate":@"inStockBeforeMaxAdvanceOrderingDate",
@"url":@"url",
@"manufacturer": @"manufacturer",
@"manufacturerName":@"manufacturerName",
@"name":@"name",
}];
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"thumbnailImageURL" withMapping:dynamicMapping]];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
if ([[representation objectForKey:@"format"] isEqualToString:@"thumbnailGrid"]) {
return ???;
}
return nil;
}];
我不确定匹配发生时从动态映射器返回什么。我是否应该创建另一个 RKEntityMapping 实例,它是同一“产品”实体的映射并将 URL 映射到 thumbnailImageURL?
以某种方式添加动态映射器作为属性而不是关系是否也可以?
谢谢,
乔巴