RestKit 0.20.1如何映射父ID [英] RestKit 0.20.1 How to map parent id

查看:97
本文介绍了RestKit 0.20.1如何映射父ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此XML有效负载:

Given this XML payload:

<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 的访问权限我在 payload.year.month

How would you get access to the yearNum from within the monthlyReportMapping when I'm mapping within the keyPath of payload.year.month?

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

Please assume that I have no control over the XML response.

谢谢,
Justyn

Thanks, Justyn

推荐答案

目前,通过元数据字典映射父ID的功能不可用,但具有0.20.3版本里程碑的活动票证:

Currently the feature of mapping the parent id via the metadata dictionary is not available but has an active ticket for the 0.20.3 release milestone:

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

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

The development branch of RestKit now lets you use @parent to access the parent node in the hierarchy or @root to access the root node in the hierarchy.

您正在遍历的层次结构基于您传递到responseDescriptor的keyPath。因此,在上面的示例中,有两件事需要做。首先创建一个新的实体 Year ,它与 MonthlyReport 到期的关系 entity(记得连接反向)。

The hierarchy you are traversing up is based on the keyPath you passed into your responseDescriptor. So in the example above there are two things that need doing. Firstly create a new entity Year that has a to-many relationship with the MonthlyReport entity (remember to connect the inverse).

现在按如下方式映射XML有效负载:

Now map the XML payload as follows:

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];

这会将年度数据映射到我们的实体然后将月份数据映射到我们的 MonthlyReport 实体。当月份数据被映射时,它可以通过@ parent键访问其parente节点。映射月份报告数据时的层次结构如下:

this will map the year data onto our Year entity and in turn then map the month data to our MonthlyReport entity. As the month data gets mapped, it has access to its parente nodes via the `@parent' key. The hierarchy at the time of mapping the month report data is this:

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

@parent.yearNum allows us to access the yearNum even though we are currently mapping a month object. The functionality also allows chaining. So if you had deeper nesting, you could do @parent.@parent.@parent.attributeKey.

这个为RestKit增加了另一层灵活性!

This adds yet another level of flexibility to RestKit!

这篇关于RestKit 0.20.1如何映射父ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆