我在这个问题上工作了很长时间,但找不到解决方案 这符合我的需要。
问题是,我如何加载数据并将它们映射到关系 没有加载整个结构
简单示例:
我们有一些鸟:
{
"birds":[{
"bird":{"id":"1","value":"LaLeLu"},
"bird":{"id":"2","value":"LeLeLa"},
...
}]
}
这可以通过以下方式映射:
RKManagedObjectMapping *birdMapping = [RKManagedObjectMapping
mappingForClass:[Bird class]];
menuMapping.primaryKeyAttribute = @"identifier";
[menuMapping mapKeyPath:@"value" toAttribute:@"value"];
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[[[RKObjectManager sharedManager] mappingProvider]
setMappingForKeyPath:"birds.bird"];
现在效果很好。
现在每只鸟都会有很多评论 - 但我并不喜欢 所有这些评论与第一个请求。
当用户点击特定的鸟时,应加载评论。
所以我要求:
NSString *resourcePath = [NSString stringWithFormat:@"/birds/%@/
comments", myBird.id]
[[RKObjectManager sharedManager]
loadObjectsAtResourcePath:resourcePath];
我可以改变它符合RestKit需求的响应 - 但是 有什么需求?
{
"comments":[{
"comment"{"id":"1","value":"Comment1","bird_id":"1"}
}]
}
现在我不知道如何映射这个响应。
映射评论与鸟类没有任何关系是没有问题的:
RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping
mappingForClass:[Comment class]];
menuMapping.primaryKeyAttribute = @"identifier";
[menuMapping mapKeyPath:@"value" toAttribute:@"value"];
[menuMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[[[RKObjectManager sharedManager] mappingProvider]
setMappingForKeyPath:"comments.comment"];
希望有人了解我的问题并提供帮助
答案 0 :(得分:0)
对所有对解决方案感兴趣的人:
RestKit 0.10.0解决问题:
RKManagedObjectMapping *commentMapping = [RKManagedObjectMapping
mappingForClass:[Comment class]];
commentMapping.primaryKeyAttribute = @"identifier";
[commentMapping mapKeyPath:@"value" toAttribute:@"value"];
[commentMapping mapKeyPath:@"id" toAttribute:@"identifier"];
// Here starts the relevant part:
[commentMapping mapKeyPath:@"bird_id" to Attribute:@"bird_id"];
[commentMapping mapRelationship:@"bird" withMapping:birdMapping];
[commentMapping connectRelationship:@"bird" withObjectPropertyForPrimaryKeyAttribute:"bird_id"]
[[[RKObjectManager sharedManager] mappingProvider]
setMapping:commentMapping ForKeyPath:"comments.comment"];