ios - Restkit mapping dependency -
is possible make mapping in restkit dependent on each other. have following json structure:
{ "idlist": [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ], “persons”: [ { "id": 2, "name": “james”, "description": “tall”, "items": [ { "id": 1051, "name": “hat”, "description": "", “accesories”: [] } ] } ] } i have 2 response descriptors, in order drill array idlist & persons.
rkresponsedescriptor *responsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:[mappingprovider personsmapping] method:rkrequestmethodget pathpattern:nil keypath:@"persons"statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [self addresponsedescriptor:responsedescriptor]; rkresponsedescriptor *idsresponsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:[mappingprovider idsmapping] method:rkrequestmethodget pathpattern:nil keypath:@"idlist" statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [self addresponsedescriptor:idsresponsedescriptor]; my mapping code:
+ (rkdynamicmapping *)idsmapping { rkentitymapping *mapping = [rkentitymapping mappingforentityforname:@"ids" inmanagedobjectstore:[[coredatamanager sharedinstance] objectstore]]; mapping.discardsinvalidobjectsoninsert = yes; [mapping addpropertymapping:[rkattributemapping attributemappingfromkeypath:nil tokeypath:@"theid"]]; rkdynamicmapping *idsmapping = [rkdynamicmapping new]; [dynamictableidsmapping setobjectmappingforrepresentationblock:^rkobjectmapping *(id representation) { if (!representation) { return nil; } else { return mapping; } return nil; }]; return dynamictableidsmapping; } + (rkdynamicmapping *)personsmapping { rkentitymapping *mapping = [rkentitymapping mappingforentityforname:@"persons" inmanagedobjectstore:[[coredatamanager sharedinstance] objectstore]]; mapping.assignsnilformissingrelationships = yes; mapping.assignsdefaultvalueformissingattributes = yes; mapping.discardsinvalidobjectsoninsert = yes; [mapping addattributemappingsfromdictionary:@{ @"id": @"remoteid", @"name": @"name" }]; mapping.identificationattributes = @[ @"remoteid" ]; [mapping addpropertymapping:[rkrelationshipmapping relationshipmappingfromkeypath:@"items" tokeypath:@"products" withmapping:[mappingprovider itemsmapping]]]; rkdynamicmapping * dynamicmenumapping = [rkdynamicmapping new]; [dynamicmenumapping setobjectmappingforrepresentationblock:^rkobjectmapping *(id representation) { if (!representation) { return nil; } else { nsarray *categoryarray = [representation valueforkey:@"items"]; if (!categoryarray || !categoryarray.count) { return nil; } else { return mapping; } } return nil; }]; return dynamicmenumapping; } how manage map persons array first , idlist? need map idlist array if persons contains values. want since user presented new view, when data in persons present, , thereby idlist. if persons array empty, not nessasary map idlist.
you need use single response descriptor nil key path. mapping response descriptor dynamic mapping checked contents , returned either mapping handled both sub-sections (so need container class) or nil (to abandon mapping process).
Comments
Post a Comment