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

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

问题描述

阅读RestKit的 GitHub上的对象映射指南后,我遇到的问题



关系实体

 公司
- unitID
- companyID
- name
- 联络人*(公司 - > ; Contact | 1:n)

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

(公司)

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

JSON(联系人)

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


b $ b

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



问题



可以将每个联系人映射到其所属公司



如果没有,我想知道最好的解决方案。感谢!







背景
$ b

在我第一次构建这个应用程序时,我将对象映射到给定的实体(没有关系),并将所有属于联系人的联系人提取到一个公司,并使用谓词 companyID =%@ 。由于数据量(4000+公司,7000+联系人)提取所有所有联系人花了大约一秒钟 - 所以我想出了使用关系(这与本地哑元数据完美)。






解决方案



在下面给出的答案的帮助下,我当前的映射看起来像(RestKit v.10)。

  //使用objectStore设置Restkit 
...

// Init类映射类公司
companyMapping = [RKManagedObjectMapping mappingForClass:[Company class] inManagedObjectStore:objectStore];
[companyMapping mapKeyPath:@unitIDtoAttribute:@unitID];
[companyMapping mapKeyPath:@companyIDtoAttribute:@companyID];
[companyMapping mapKeyPath:@nametoAttribute:@name];
companyMapping.setDefaultValueForMissingAttributes = NO;
companyMapping.primaryKeyAttribute = @companyID;

//初始化objectMapping for类Contact
contactMapping = [RKManagedObjectMapping mappingForClass:[Contact class] inManagedObjectStore:objectStore];
[contactMapping mapKeyPath:@unitIDtoAttribute:@unitID];
[contactMapping mapKeyPath:@companyIDtoAttribute:@companyID];
[contactMapping mapKeyPath:@contactIDtoAttribute:@contactID];
[contactMapping mapKeyPath:@lastNametoAttribute:@lastName];
[contactMapping mapKeyPath:@firstNametoAttribute:@firstName];
contactMapping.setDefaultValueForMissingAttributes = NO;
contactMapping.primaryKeyAttribute = @contactID;

//初始关系
[contactMapping mapRelationship:@companywithMapping:companyMapping];
[contactMapping connectRelationship:@companywithObjectForPrimaryKeyAttribute:@companyID];

//从服务器获取对象
...

解决方案

是的,RestKit可以在映射时为您加入这种关系。在您的RKManagedObjectMapping为您的Contact模型,您将需要执行以下操作:




  • 添加关系映射到母公司: [objectMapping mapRelationship:@companywithMapping:companyMapping]

  • 将companyID属性映射到您的Contact模型

  • 确保您已在两个类上配置primaryKeyAttribute

  • 指示RestKit使关系更加流畅: [objectMapping connectRelationship:@companywithObjectForPrimaryKeyAttribute:@companyID ];



RestKit完成了一个有效载荷的映射之后,关系。处理被推迟到映射之后,以便有效负载实际创建您需要满足连接的对象。



在开发分支上,有一个可供选择的工作流程。本周早些时候,我在有效载荷中使用ID数组来增加了对一个多对多关系进行水化的支持。这将允许您发送属于公司的联系人的列表在有效载荷中。



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


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.

Entities with Relationships

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

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

JSON (Company)

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

JSON (Contact)

 [
   {
      "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.

Question

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

If not, I would like to know the best solution for it. Thanks!


Background

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).


Solution

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
...

解决方案

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:

  • Add a relationship mapping to the parent Company: [objectMapping mapRelationship:@"company" withMapping:companyMapping]
  • Map the companyID attribute onto your Contact model
  • Ensure that you have the primaryKeyAttribute configured on both classes
  • Instruct RestKit to hydrate the relationship: [objectMapping connectRelationship:@"company" withObjectForPrimaryKeyAttribute:@"companyID"];

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.

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.

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

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

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