0

我在这个问题上工作了很长时间,但找不到适合我需要的解决方案。

问题是,如何在不加载整个结构的情况下加载数据并将它们映射到关系

简单的例子:

我们得到了一些鸟:

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

希望有人理解我的问题并可以提供帮助

4

1 回答 1

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"]; 
于 2012-04-06T14:06:44.323 回答