Restkit映射依赖关系

是否有可能使restkit中的映射相互依赖。 我有以下的JSON结构:

{ "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”: [] } ] } ] } 

我有2个响应描述符,以钻入数组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]; 

我的映射代码:

 + (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; } 

我将如何设法映射人员数组,然后IdList? 我只需要映射idList数组,如果个人包含值。 我想这样做,因为用户只是提供一个新的视图,当数据存在人员,因此也是idList。 所以如果persons数组是空的,那么映射IdList并不是必须的。

您将需要使用一个单一的响应描述符与零键path。 该响应描述符的映射将是一个dynamic映射,它检查内容,并返回一个处理两个子部分(所以你需要一个容器类)或零(放弃映射过程)的映射。