そこで、RestKit 0.20(pre2)を使用してこれを理解しました。
JSONを次のように変更する必要がありました(グループのusers配列の属性名に注意してください)。
{
"users": [
{"id": "1", "name": "John Doe"},
{"id": "2", "name": "Bill Nye"}
],
"groups": [
{"id": "1", "name": "Group1", "users": [{"id" : "1"}, {"id" : "2"}]},
{"id": "2", "name": "Group2", "users": [{"id" : "1"}]}
]
}
次に、次のマッピング:
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[@"id"];
[userMapping addAttributeMappingsFromArray:@[@"id", @"name"]];
RKEntityMapping *groupMapping = [RKEntityMapping mappingForEntityForName:@"Group" inManagedObjectStore:managedObjectStore];
groupMapping.identificationAttributes = @[@"id"];
[groupMapping addAttributeMappingsFromArray:@[@"id", @"name"]];
[groupMapping addRelationshipMappingWithSourceKeyPath:@"users" mapping:userMapping];
そして最後に、次のresponseDescriptors:
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:classMapping pathPattern:@"/api/allthejson" keyPath:@"users" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *groupResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:classMapping pathPattern:@"/api/allthejson" keyPath:@"groups" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsArray:@[userResponseDescriptor, groupResponseDescriptor]];
次に、RKObjectManagerのgetObjectsAtPath:parameters:success:failureメソッドを使用してオブジェクトを取得します。