RestKit 0.20.1 Comment mapper l'identifiant parent

Compte tenu de cette charge utile XML:

<payload> <year yearNum="2013"> <month monthNum="6" desc="This month was an enlightening month"/> <month monthNum="5" desc="This month was a questioning month"/> <month monthNum="4" desc="This month was a good month"/> <month monthNum="3" desc="This month was a crazy month"/> <month monthNum="2" desc="This month was a dry month"/> <month monthNum="1" desc="This month was a slow month"/> </year> <year yearNum="2012"> <month monthNum="12" desc="This month was a cold month"/> <month monthNum="11" desc="This month was an expensive month"/> <month monthNum="10" desc="This month was a free month"/> <month monthNum="9" desc="This month was a hard month"/> <month monthNum="8" desc="This month was a surprising month"/> <month monthNum="7" desc="This month was an energetic month"/> <month monthNum="6" desc="This month was a hasty month"/> <month monthNum="5" desc="This month was a relaxing month"/> <month monthNum="4" desc="This month was a fair month"/> <month monthNum="3" desc="This month was a strange month"/> <month monthNum="2" desc="This month was a lucky month"/> <month monthNum="1" desc="This month was a odd month"/> </year> </payload> 

et une cartographie de:

 RKEntityMapping *monthlyReportMapping = [RKEntityMapping mappingForEntityForName:@"MonthlyReport" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]]; monthlyReportMapping.identificationAtsortingbutes = @[@"yearNumber", @"monthNumber"]]; [monthlyReportMapping addAtsortingbuteMappingsFromDictionary:@{ /* * How would I set up the mappings for the yearNumber * so I can use it as the composite identifier with * the monthNumber? I want to do something like this: */ @"@metadata.parent.yearNum" : @"yearNumber", @"monthNum" : @"monthNumber", @"desc" : @"description" }]; RKResponseDescriptor *monthlyMappingResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:monthlyReportMapping pathPattern:@"/monthlyReports" keyPath:@"payload.year.month" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [[RKObjectManager sharedManager] addResponseDescriptor:monthlyMappingResponseDescriptor]; 

Comment auriez-vous access à yearNum partir de la fonction monthlyReportMapping lorsque je suis en train de mapper dans le keyPath de payload.year.month ?

Veuillez supposer que je n'ai aucun contrôle sur la réponse XML.

Merci, Justyn

Actuellement, la fonctionnalité de mappage de l'ID parent via le dictionary de métadonnées n'est pas disponible mais possède un ticket actif pour le jalon de la version 0.20.3:

https://github.com/RestKit/RestKit/issues/1327

Mettre à jour

La twig de développement de RestKit vous permet maintenant d'utiliser @parent pour accéder au nœud parent dans la hiérarchie ou à @root pour accéder au nœud racine dans la hiérarchie.

La hiérarchie que vous parcourez est basée sur le keyPath que vous avez passé dans votre responseDescriptor. Donc, dans l'exemple ci-dessus, il y a deux choses à faire. Créez d'abord une nouvelle entité Year qui a une relation to-many avec l'entité MonthlyReport (n'oubliez pas de connecter l'inverse ).

Maintenant mapper la charge utile XML comme suit:

 RKEntityMapping *yearMapping = [RKEntityMapping mappingForEntityForName:@"Year" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]]; yearMapping.identificationAtsortingbutes = @[@"yearNumber"]]; [yearMapping addAtsortingbuteMappingsFromDictionary:@{ @"yearNum" : @"yearNumber" }]; RKEntityMapping *monthlyReportMapping = [RKEntityMapping mappingForEntityForName:@"MonthlyReport" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]]; monthlyReportMapping.identificationAtsortingbutes = @[@"monthYearNumber", @"monthNumber"]]; [monthlyReportMapping addAtsortingbuteMappingsFromDictionary:@{ @"@parent.yearNum" : @"monthYearNumber", @"monthNum" : @"monthNumber", @"desc" : @"monthDescription" }]; // Map the keyPath of `month` to our coredata entity // relationship `months` using our monthReportMapping [yearMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"month" toKeyPath:@"months" withMapping:monthlyReportMapping]]; // Notice how the keyPath now points to payload.year RKResponseDescriptor *monthlyReportMappingResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:yearMapping pathPattern:@"/monthlyReports" keyPath:@"payload.year" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [[RKObjectManager sharedManager] addResponseDescriptor:monthlyReportMappingResponseDescriptor]; 

Quand nous appelons alors:

 [[RKObjectManager sharedManager] getObjectsAtPath:@"/monthlyReports" parameters:nil success:nil failure:nil]; 

cela permettra de cartographier datatables de l'année sur notre entité Year et, à son tour, de cartographier datatables du mois à notre entité MonthlyReport . Au fur et à mesure que datatables du mois sont mappées, il a access à ses nœuds de parente via la key `@parent '. La hiérarchie au moment de la mise en correspondance des données du rapport de mois est la suivante:

 yearNum: @2013 [ month { // <-- Currently mapping the month. // We used to only get to see what was inside // this with no access to the parent nodes. monthNum: @6, desc: @"This month was an enlightening month" }, month { monthNum: @5, desc: @"This month was a questioning month" }, … ]; 

@parent.yearNum nous permet d'accéder au yearNum même si nous sums en train de mapper un object month. La fonctionnalité permet également de stringr. Donc, si vous avez une imbrication plus profonde, vous pouvez faire @parent.@[email protected] . @parent.@[email protected] .

Cela ajoute encore un autre niveau de flexibilité à RestKit!