假设你有JSON像上面,你将不得不增加一个属性对应一个特定的评论属于这样的,我们的JSON看起来像哪个事件,
"comments": [
{
"id": 23,
"user_id": 9,
"commentable_id": 12,
"commentable_type": "Event",
"content": "This is the content of the event",
"created_at": "2013-04-19 19:28:42.533901",
"updated_at": "2013-04-19 19:28:42.533901",
"event_id": "10", /* Note this is a new attribute to map to the parent entity */
}
]
现在,添加一个新的属性给你的评论模型,这样这个event_id将被存储。我们创建一个属性eventId。
所以,让我们创建映射为实体,
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping setIdentificationAttributes:@[@"identifier"]];
[mapping addAttributeMappingsFromDictionary:@{
@"id": @"identifier",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"user_id": @"userId",
@"commentable_id": @"commentableId",
@"commentable_type": @"commentableType",
@"content": @"content",
@"event_id": @"eventId"
}];
然后,我们将不得不增加连接映射事项标识纠正事件,
NSRelationshipDescription *eventRelationship = [[mapping entity] relationshipsByName][@"event"];
[mapping addConnection:[[RKConnectionDescription alloc] initWithRelationship:eventRelationship attributes:@{@"eventId": @"identifier"}]]; // this line says that you have to have eventId in your comments entity and then the identifier in event entity to which the eventId will be mapped to.
就是这样,你可以现在创建RKResponseDescriptor和RKManagedObjectRequestOperation来提取评论,它将指向正确的事件。
你看过RKConnectionDescription。它允许您将注释独立映射到父模型。你的JSON看起来如何。如果你寄给我样品,我可能会向你提供答案。 – Sandeep
我添加了一个典型的'JSON'响应。如果我将这些注释独立映射到父项,那么在映射之后如何将它们加入到父项中?无论如何,谢谢你这么快的回答。 – BriceB