RestKit Core Data mapping connection with foreign key in the URL

二次信任 提交于 2019-12-08 07:42:50

问题


I have an API with two endpoints:

/projects

{
  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"}]
}

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.

Current code:

// ###############################
// ## 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 ]];

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.

How do I connect these when the project foreign key is in the URL and not returned in the JSON data when retrieving the members?


回答1:


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

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.

Define your path pattern like:

projects/:identity/members

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

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

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



来源:https://stackoverflow.com/questions/16734694/restkit-core-data-mapping-connection-with-foreign-key-in-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!