鉴于此XML有效负载:
<payload>
<year yearNum="2013">
<month monthNum="6" desc="This month was an enlightening month"/>
<month monthNum="5" desc="This month was a questioning month"/>
<month monthNum="4" desc="This month was a good month"/>
<month monthNum="3" desc="This month was a crazy month"/>
<month monthNum="2" desc="This month was a dry month"/>
<month monthNum="1" desc="This month was a slow month"/>
</year>
<year yearNum="2012">
<month monthNum="12" desc="This month was a cold month"/>
<month monthNum="11" desc="This month was an expensive month"/>
<month monthNum="10" desc="This month was a free month"/>
<month monthNum="9" desc="This month was a hard month"/>
<month monthNum="8" desc="This month was a surprising month"/>
<month monthNum="7" desc="This month was an energetic month"/>
<month monthNum="6" desc="This month was a hasty month"/>
<month monthNum="5" desc="This month was a relaxing month"/>
<month monthNum="4" desc="This month was a fair month"/>
<month monthNum="3" desc="This month was a strange month"/>
<month monthNum="2" desc="This month was a lucky month"/>
<month monthNum="1" desc="This month was a odd month"/>
</year>
</payload>
以及:
的映射RKEntityMapping *monthlyReportMapping =
[RKEntityMapping mappingForEntityForName:@"MonthlyReport"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
monthlyReportMapping.identificationAttributes = @[@"yearNumber", @"monthNumber"]];
[monthlyReportMapping addAttributeMappingsFromDictionary:@{
/*
* How would I set up the mappings for the yearNumber
* so I can use it as the composite identifier with
* the monthNumber? I want to do something like this:
*/
@"@metadata.parent.yearNum" : @"yearNumber",
@"monthNum" : @"monthNumber",
@"desc" : @"description"
}];
RKResponseDescriptor *monthlyMappingResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:monthlyReportMapping
pathPattern:@"/monthlyReports"
keyPath:@"payload.year.month"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:monthlyMappingResponseDescriptor];
当我在yearNum
的keyPath中进行映射时,如何从monthlyReportMapping
内访问payload.year.month
?
请假设我无法控制XML响应。
谢谢, Justyn
答案 0 :(得分:4)
目前,通过元数据字典映射父ID的功能不可用,但具有0.20.3版本里程碑的活动票证:
https://github.com/RestKit/RestKit/issues/1327
RestKit的development branch现在允许您使用@parent
访问层次结构中的父节点或@root
以访问层次结构中的根节点。
您正在遍历的层次结构基于您传递到responseDescriptor的keyPath。因此,在上面的示例中,有两件事需要做。首先创建一个与Year
实体有to-many
关系的新实体MonthlyReport
(请记住connect the inverse)。
现在按如下方式映射XML有效负载:
RKEntityMapping *yearMapping =
[RKEntityMapping mappingForEntityForName:@"Year"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
yearMapping.identificationAttributes = @[@"yearNumber"]];
[yearMapping addAttributeMappingsFromDictionary:@{
@"yearNum" : @"yearNumber"
}];
RKEntityMapping *monthlyReportMapping =
[RKEntityMapping mappingForEntityForName:@"MonthlyReport"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
monthlyReportMapping.identificationAttributes = @[@"monthYearNumber", @"monthNumber"]];
[monthlyReportMapping addAttributeMappingsFromDictionary:@{
@"@parent.yearNum" : @"monthYearNumber",
@"monthNum" : @"monthNumber",
@"desc" : @"monthDescription"
}];
// Map the keyPath of `month` to our coredata entity
// relationship `months` using our monthReportMapping
[yearMapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"month"
toKeyPath:@"months"
withMapping:monthlyReportMapping]];
// Notice how the keyPath now points to payload.year
RKResponseDescriptor *monthlyReportMappingResponseDescriptor
= [RKResponseDescriptor responseDescriptorWithMapping:yearMapping
pathPattern:@"/monthlyReports"
keyPath:@"payload.year"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager]
addResponseDescriptor:monthlyReportMappingResponseDescriptor];
然后我们打电话:
[[RKObjectManager sharedManager]
getObjectsAtPath:@"/monthlyReports" parameters:nil success:nil failure:nil];
这会将年度数据映射到我们的Year
实体,然后将月份数据映射到我们的MonthlyReport
实体。当月数据被映射时,它可以通过`@parent'键访问其parente节点。映射月份报告数据时的层次结构如下:
yearNum: @2013
[
month { // <-- Currently mapping the month.
// We used to only get to see what was inside
// this with no access to the parent nodes.
monthNum: @6,
desc: @"This month was an enlightening month"
},
month {
monthNum: @5,
desc: @"This month was a questioning month"
},
…
];
@parent.yearNum
允许我们访问yearNum
,即使我们当前正在映射月份对象。该功能还允许链接。因此,如果你有更深的嵌套,你可以做@parent.@parent.@parent.attributeKey
。
这为RestKit增加了另一层灵活性!