假設你有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