没有 KVC 的 RestKit 对象映射关系 [英] RestKit Object Mapping Relationships without KVC

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

问题描述

阅读 RestKit 的 GitHub 上的对象映射指南后,我的问题没有不会消失,所以也许有人会说 RestKit 是否可以处理以下想法.

After reading the Object Mapping-Guide on GitHub for RestKit my problem didn't disappear, so perhaps somebody can say if RestKit could deal with the following idea.

有关系的实体

 Company 
 - unitID 
 - companyID
 - name
 - contacts* (Company -->> Contact | 1:n)

 Contact
 - unitID
 - companyID
 - contactID
 - lastName
 - firstName
 - account* (Contact >--> Company | 1:1)

JSON(公司)

 [
   {
      "unitID":"003CABD8DEB5DC13C",
      "companyID":"BSP-002999",
      "name":"Testcompany"
   }
 ]

JSON(联系方式)

 [
   {
      "unitID":"DAC2ACCC125795D00",
      "companyID":"BSP-002999",
      "contactID":"CLP-015468",
      "firstName":"Mister",
      "lastName":"Wayne"
   }
 ]

由于限制,我无法将所属联系人嵌套到公司中(否则我不会写这个),所以我想在导入时映射数据时使用.

Due to limitations I'm not able to nest the belonging contacts into the companies (otherwise I wouldn't write this), so I want to do this use when the data is mapped on import.

是否可以在导入时使用 RestKit 的给定方法将每个联系人映射到它所属的公司(由属性 companyID 标识)?

Is it possible to map each Contact to it's belonging Company (identified by the attribute companyID) on import with given methods by RestKit?

如果没有,我想知道它的最佳解决方案.谢谢!

背景

在这个应用程序的第一个版本中,我将对象映射到给定的实体(没有关系),并使用谓词 companyID = %@ 获取所有属于公司的联系人.由于数据量大(4000 多家公司、7000 多个联系人),获取所有联系人大约需要一秒钟 - 所以我想出了使用关系的想法(它与本地虚拟数据完美结合).

In my first build of this app I've mapped the objects to the given entities (without relationships) and fetched all belonging contacts to a company with the predicate companyID = %@. Due to the amount of data (4000+ Companies, 7000+ Contacts) fetching all belonging contacts takes about a second - so I came up with the idea to use relationsships (which works perfect with local dummy data).

在下面给出的答案的帮助下,我当前的映射如下所示(RestKit v.10).

With the help of the given answer below my current mapping looks like the following (RestKit v.10).

// Setting up Restkit with objectStore
...

// Init objectMapping for Class Company
companyMapping = [RKManagedObjectMapping mappingForClass:[Company class] inManagedObjectStore:objectStore];
[companyMapping mapKeyPath:@"unitID" toAttribute:@"unitID"];
[companyMapping mapKeyPath:@"companyID" toAttribute:@"companyID"];
[companyMapping mapKeyPath:@"name" toAttribute:@"name"];
companyMapping.setDefaultValueForMissingAttributes = NO;
companyMapping.primaryKeyAttribute = @"companyID";

// Init objectMapping for Class Contact
contactMapping = [RKManagedObjectMapping mappingForClass:[Contact class] inManagedObjectStore:objectStore];
[contactMapping mapKeyPath:@"unitID" toAttribute:@"unitID"];
[contactMapping mapKeyPath:@"companyID" toAttribute:@"companyID"];
[contactMapping mapKeyPath:@"contactID" toAttribute:@"contactID"];
[contactMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[contactMapping mapKeyPath:@"firstName" toAttribute:@"firstName"];
contactMapping.setDefaultValueForMissingAttributes = NO;
contactMapping.primaryKeyAttribute = @"contactID";

// Init relationships
[contactMapping mapRelationship:@"company" withMapping:companyMapping];
[contactMapping connectRelationship:@"company" withObjectForPrimaryKeyAttribute:@"companyID"];

// Get objects from server
...

推荐答案

是的,RestKit 可以在映射时为您补充这种关系.在您的联系人模型的 RKManagedObjectMapping 上,您需要执行以下操作:

Yes, RestKit can hydrate this relationship for you at mapping time. On your RKManagedObjectMapping for your Contact model, you will need to do the following:

  • 添加到父公司的关系映射:[objectMapping mapRelationship:@"company" withMapping:companyMapping]
  • 将 companyID 属性映射到您的联系人模型
  • 确保在两个类上都配置了 primaryKeyAttribute
  • 指示 RestKit 对关系进行水合:[objectMapping connectRelationship:@"company" withObjectForPrimaryKeyAttribute:@"companyID"];

RestKit 完成映射有效负载后,它将通过集合传递回并水合任何关系.处理被推迟到映射之后,以防负载实际创建您需要满足连接的对象.

After RestKit has completed mapping a payload, it will pass back through the collection and hydrate any relationships. The processing is deferred until after mapping in case the payload actually creates objects that you need to satisfy the connections.

在开发分支上,您可以使用替代工作流程.本周早些时候,我添加了对使用有效载荷中的一组 ID 来增强多对多关系的支持.这将允许您将属于公司的联系人列表发送到有效负载中.对于您的用例,它可能更有效.

On the development branch, there is an alternative workflow available to you. Earlier this week I added support for hydrating a has-many relationship using an array of ID's in the payload. This would let you send the list of Contacts that belong to a company down in the payload instead. It may be more efficient for your use case.

从我的帖子到 RestKit 邮件列表的其他详细信息:http://groups.google.com/group/restkit/msg/416951f86b2862d1

Additional details from my post to the RestKit mailing list: http://groups.google.com/group/restkit/msg/416951f86b2862d1

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

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