在RestKit中放置对象映射的最佳位置在哪里 [英] Where's the best place to put object mappings in RestKit

查看:81
本文介绍了在RestKit中放置对象映射的最佳位置在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目使用RestKit,我注意到该类中不再存在可以控制(elementToPropertyMappings)中所有映射的方法,因此我想知道放置新代码的最佳位置在哪里我正在视图控制器中执行此操作,但是我将在代码的其他区域中使用大多数相同的映射,因此可以在其中放置更有效的地方:

I'm using RestKit for a project and i noticed that there is no longer a method in the class that you can control all mappings in (elementToPropertyMappings), i therefore wondered where the best place to put the new code was, currently i'm doing it in my view controller, but i'll be using most of the same mappings in other areas of my code so is there a more efficient place to put it:

我所指的代码是:

RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[userMapping mapKeyPath:@"forename" toAttribute:@"forename"];
[userMapping mapKeyPath:@"surname" toAttribute:@"surname"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[userMapping mapKeyPath:@"twitter_username" toAttribute:@"twitterUsername"];
[userMapping mapKeyPath:@"created" toAttribute:@"created"];
[userMapping mapKeyPath:@"use_gravatar" toAttribute:@"useGravatar"];
[userMapping mapKeyPath:@"avatar_url" toAttribute:@"avatarURL"];
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"];

如果这可以在User类的方法中,然后我可以调用该方法来设置这些映射等,那就太好了.

It would be great if this could be in a method on the User class that i can then call to setup these mappings etc.

非常感谢

推荐答案

目前建议使用一些好的方法来组织映射:

There are a few good ways to organize your mappings currently recommended:

  1. 将所有映射放入您的应用程序委托中,并在应用程序初始化时对其进行配置.这样可以将它们排除在控制器之外,如果您的映射数量较少,则很有用.
  2. 子类化RKObjectMappingProvider,并在子类的init方法中构建所有映射,然后实例化映射提供者实例并将其分配给对象管理器.
  3. 使用类似于"defineMappings"的方法向RKObjectMappingProvider添加类别.然后,在初始化RestKit之后,将标头导入到您的应用程序委托中,并通过[mappingProvider defineMappings]调用它.

在类方法上定义映射的问题在于,当您具有关系时,由于没有可用的ivar来存储实例,因此可能会循环依赖.我们也许可以用块做些什么来帮助减轻这个问题,但是还没有完成这样的工作.

The problem with defining mappings on a class method is that when you have relationships, you can wind up with circular dependencies as there's no ivars available to store the instances. We may be able to do something with blocks to help alleviate this problem, but no such work has been done yet.

这篇关于在RestKit中放置对象映射的最佳位置在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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