我有一个带有两个端点的API:
/项目
{
project_list:[{"project_id": 1, "project_name": "Ben"},
{"project_id": 2, "project_name": "Jerry"}]
}
/项目/ 1 /成员信息
{
member_list:[{"member_id": 1, "member_name": "Ben"},
{"member_id": 2, "member_name": "Jerry"}]
}
项目有很多成员,成员有一个项目。我正在使用RestKit 0.20.1将这些映射到Core Data存储。
当前代码:
// ###############################
// ## 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响应中的外键。
如果项目外键在URL中并且在检索成员时未在JSON数据中返回,如何连接这些?
答案 0 :(得分:1)
您可以使用您链接的参考文献中描述的外键,您只需要获取它......
要获得它,您需要使用路径模式和元数据映射。您还希望使用RKRoute
,因为它使用路径模式进行了大量工作。
定义您的路径模式,如:
projects/:identity/members
然后在你的映射中你可以定义外键来选择它:
@"@metadata.routing.parameters.identity": @"foreignKey",
请参阅RKObjectManager docs的<元数据映射'部分。