问题
Is it possible to make the mapping in restkit dependent on each other. I have the 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 to drill into the 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 would I manage to map the Persons array first and then the IdList? I only need to map the idList array if persons contains values. I want do this since the user is only presented for a new view, when data in Persons is present, and thereby also the idList. So if the persons array is empty, it is not nessasary to map the IdList.
回答1:
You would need to use a single response descriptor with a nil key path. The mapping for that response descriptor would be a dynamic mapping which checked the contents and returned either a mapping which handled both sub-sections (so you would need a container class) or nil (to abandon the mapping process).
来源:https://stackoverflow.com/questions/22627332/restkit-mapping-dependency