RestKit 0.20.1 如何映射父id

2023-11-29

给定这个 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从内部monthlyReportMapping当我在 keyPath 内映射时payload.year.month?

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

谢谢, 贾斯汀


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

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

Update

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

您要遍历的层次结构基于您传递到responseDescriptor 中的keyPath。所以在上面的例子中,有两件事需要做。首先创建一个新实体Year有一个to-many与的关系MonthlyReport实体(记住连接逆).

现在映射 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.yearNum允许我们访问yearNum即使我们当前正在映射一个月对象。该功能还允许链接。所以如果你有更深的嵌套,你可以这样做@parent.@[email protected].

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RestKit 0.20.1 如何映射父id 的相关文章

随机推荐

  • C++ Win32静态控制透明背景

    解决方案 如下所述 最好为文本创建自己的方法 而不是尝试让控件表现异常 因此 为此创建一个自定义控件是最好的 我找到了一个解释这一切的教程 http www codeproject com Articles 559385 Custom Co
  • 是什么导致 randomForest.partialPlot 函数出现这种奇怪的行为?

    我正在使用randomForestR 2 15 2 中的包 v 4 6 7 我找不到该程序的源代码partialPlot函数 我试图弄清楚它到底做了什么 帮助文件似乎不完整 它应该采用变量的名称x var作为一个论点 library ran
  • 关闭 tomcat 时停止计划的计时器[重复]

    这个问题在这里已经有答案了 我有一个部署到 Tomcat 服务器的 WAR 文件 其中一个类将在启动时被调用 然后 init 方法将安排一个计时器每 5 小时触发一次以执行一些任务 我的 init 代码如下所示 public void in
  • 如何使用 .htaccess 从 url 中删除 %20

    如何删除 20 还有更多来自 htaccess 的 url 我已经尝试了这篇文章中的代码 但仍然没有替换 重定向到新的网址 htaccess url 重写并删除 20 这是我的 htaccess 代码 RewriteEngine On Re
  • 仅使用一个 dbContext 对多个数据库执行投影

    我们正在将 EF Core 用于当前项目 该项目有三个数据库 这是一个真正无法避免的麻烦 数据库具有相同的结构 因此我们能够交换上下文并使用相同的 EF 模型来执行 CRUD 操作 我们有一个特别复杂的查询 我们将从传统的 ADO NET
  • 如何在iPad中实现文字转语音功能?

    是否有任何 3rd 方 API 可以提供文本转语音功能 Yes The CMU Flite 节日精简版 语音合成库已移植到iOS并且似乎在 App Store 中的许多 iPhone 和 iPad 应用程序中使用
  • 如何从 T 中减去 1,其中 T: num 特征::Num?

    我正在尝试创建一个适用于所有数字整数类型的特征 我认为限制它num traits Num将强制它仅适用于 u8 u16 等 因此 1 始终有效 pub fn divide round up
  • 在生产环境中,Socket.io websocket 在 Nuxt 3 中无法工作

    我正在 Nuxt 3 应用程序中创建一个 socket io 实现 当我处于开发模式时 Websocket 可以工作 但出现此错误错误信息 我使用的是 Nuxt 版本 nuxt v3 0 0 rc 8 这是我的 nuxt config ts
  • 将数据从一个表插入到另一个表

    我有两个不同的表 但列的命名略有不同 我想从一张表中获取信息并将其放入另一张表中 仅当表 1 中的 信息字段 不为空时 我才需要将表 1 中的信息放入表 2 中 每当创建某些内容时 表 2 都有一个唯一的 ID 因此插入的任何内容都需要获取
  • 使用单连接实例实现 Eclipse MQTT Android 客户端

    我在我的应用程序中使用 Eclipse Paho android mqtt 服务 我能够订阅消息并将消息发布到 mqtt 代理 我的应用程序中有几个活动 当任何活动启动时 它都会使用以下方式连接到代理mqttAndroidClient co
  • PowerShell:.NET 程序集的导入模块或添加类型?

    我使用的是 PowerShell 5 1 Windows 10 x64 我应该使用这 2 个 cmdlet 中的哪一个将 NET 程序集 特别是 NET Framework 4 程序集 加载到 PowerShell 中 他们之间的核心区别是
  • 如何使用 VBScript 或批处理文件下载 JSON 文件并获取值?

    这是回答的 VBScript 代码here从计算机获取具有正确值的 JSON 文件 Set fso CreateObject Scripting FileSystemObject json fso OpenTextFile C path t
  • 温莎城堡:- 通过配置注入接口字典

    您好 我正在尝试注入接口字典 但从城堡中收到如下错误 Castle MicroKernel SubSystems Conversion ConverterException 没有注册转换器来处理 IFoo 类型 为了解决该异常 我必须创建一
  • 如何获得 url 更改的提示

    我是 gwt 的新人 我希望当用户按下后退按钮时 他会收到一个警报 表明我们将刷新页面 大多数情况下 我们会在松散焦点打字时看到 如果用户按下后退按钮 他的页面就会刷新 并且所有值都会被清洗 我们怎样才能做到这一点 Try 窗口 Closi
  • Spring Cloud Config:客户端不会尝试连接到配置服务器

    我正在尝试创建一个简单的 Spring Cloud Config 服务器 客户端设置 并且大致遵循文档 https cloud spring io spring cloud config reference html 到目前为止 我已经实现
  • HTML 5 中的视频捕获

    如何从视频设备捕获视频并使该视频在 HTML5 画布中可用 也许你应该使用HTML5视频捕捉功能 这种方式不需要安装任何插件 而是告诉用户升级他们的浏览器
  • Summernote - 从服务器删除图像

    您好 我已使用以下链接中的代码来允许将图像上传到服务器 Summernote 图片上传 如果用户从编辑器中删除图像 是否可以实现类似的操作以从服务器中删除图像 如果是这样 我该如何实现这一目标 要从服务器删除文件 您需要使用onMediaD
  • jTable 根据数据所有者条件显示\隐藏编辑和删除按钮

    我使用 jTable 来显示 CD 信息 并使用子表来显示该 CD 的评论 我希望能够仅在登录用户的行上显示编辑 删除按钮 我一直在尝试遵循以下建议 https github com hikalkan jtable issues 113 h
  • Method Of类型不能用来调用该方法

    我想动态生成一些测试 因为我必须调用一个带有要调用的方法名称的方法 然后完成所有测试设置并调用该方法 所以基本上我打电话createTest methodName 代替it methodName gt lotsOfBoringStuff 为
  • RestKit 0.20.1 如何映射父id

    给定这个 XML 负载