URL中带有外键的RestKit Core数据映射连接 [英] RestKit Core Data mapping connection with foreign key in the URL

查看:86
本文介绍了URL中带有外键的RestKit Core数据映射连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个端点的API:

I have an API with two endpoints:

/项目

{
  project_list:[{"project_id": 1, "project_name": "Ben"},
                {"project_id": 2, "project_name": "Jerry"}]
}

/ projects / 1 / members

{
  member_list:[{"member_id": 1, "member_name": "Ben"},
               {"member_id": 2, "member_name": "Jerry"}]
}



<一个项目有很多成员,而一个成员有一个项目。我正在使用RestKit 0.20.1将它们映射到Core Data存储。

A project has-many members and a member has-one project. I'm using RestKit 0.20.1 to map these to a Core Data store.

当前代码:

// ###############################
// ## Configure Project mapping ##
// ###############################
RKEntityMapping *projectMapping = [RKEntityMapping mappingForEntityForName:@"Project" inManagedObjectStore:managedObjectStore];
projectMapping.identificationAttributes = @[ @"projectId" ];
[projectMapping addAttributeMappingsFromDictionary:@{ @"project_id": @"projectId",
                                                    @"project_name": @"projectName"}];

RKResponseDescriptor *projectDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:projectMapping pathPattern:nil keyPath:@"project_list" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptorsFromArray:@[ projectDescriptor ]];

// ##############################
// ## Configure Member mapping ##
// ##############################
RKEntityMapping *memberMapping = [RKEntityMapping mappingForEntityForName:@"Member" inManagedObjectStore:managedObjectStore];
memberMapping.identificationAttributes = @[ @"memberId" ];
[memberMapping addAttributeMappingsFromDictionary:@{ @"member_id": @"memberId",
                                                   @"member_name": @"memberName"}];

RKResponseDescriptor *memberDescriptior = [RKResponseDescriptor responseDescriptorWithMapping:memberMapping pathPattern:nil keyPath:@"member_list" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptorsFromArray:@[ memberDescriptor ]];

我最接近的是以下示例: http://restkit.org/api/latest/Classes/RKConnectionDescription.html ,但它们取决于外键在JSON响应。

The closest I've come are these examples: http://restkit.org/api/latest/Classes/RKConnectionDescription.html, but they depend on the foreign key being in the JSON response.

当检索成员时,如何在项目外键位于URL中且未在JSON数据中返回的情况下连接这些链接?

推荐答案

您可以按照链接参考中的描述使用外键,只需要获取它...

You can use the foreign key as described in the reference you link, you just need to get it...

要获取它,您需要使用路径模式和元数据映射。您还想使用 RKRoute ,因为它会处理很多路径模式。

To get it, you need to use a path pattern and metadata mapping. You also want to use RKRoute as it does a lot of the work with path patterns.

定义路径模式例如:

projects/:identity/members

然后在映射中可以定义外键来提取它:

And then in your mapping you can define the foreign key to pick this up:

@"@metadata.routing.parameters.identity": @"foreignKey",

请参见 RKObjectManager文档中的元数据映射部分。

See the 'Metadata Mapping' section of the RKObjectManager docs.

这篇关于URL中带有外键的RestKit Core数据映射连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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