2012-04-04 64 views
0

我工作的這個問題很長一段時間,但無法找到一個解決方案 適合我的需要的後期加載。遠程數據(RestKit和CoreData)

的問題是,我怎麼會加載數據,並將它們映射到關係 無需加載整個結構

簡單的例子:

我們得到了一些鳥:

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

現在很好用。

現在每隻鳥可能有很多的意見 - 但我不wanne負載 所有與第一請求這些意見。當用戶點擊特定的鳥

評論應該得到加載。

所以我要求:

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

對於所有誰潔具感興趣的解決方案:

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