1

鉴于此 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];

当我在 keyPath 中映射时,您将如何访问yearNumfrom ?monthlyReportMappingpayload.year.month

请假设我无法控制 XML 响应。

谢谢,贾斯汀

4

1 回答 1

4

目前,通过元数据字典映射父 ID 的功能不可用,但有 0.20.3 发布里程碑的有效票证:

https://github.com/RestKit/RestKit/issues/1327

更新

RestKit的开发分支现在允许您@parent访问层次结构中的父节点或@root访问层次结构中的根节点。

您正在遍历的层次结构基于您传递给您的 responseDescriptor 的 keyPath。所以在上面的例子中有两件事需要做。首先创建一个与该实体Yearto-many关系的新MonthlyReport实体(记住要连接 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”键访问其父节点。映射月报表数据时的层次结构是这样的:

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.yearNumyearNum即使我们当前正在映射月份对象,也允许我们访问。该功能还允许链接。所以如果你有更深的嵌套,你可以做@parent.@parent.@parent.attributeKey.

这为 RestKit 增加了另一个级别的灵活性!

于 2013-06-06T09:03:20.933 回答