Restkit映射依赖项

时间:2014-03-25 06:52:47

标签: ios objective-c restkit restkit-0.20

是否可以使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&人

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

我如何设法首先映射Persons数组然后映射IdList?如果人员包含值,我只需要映射idList数组。我想要这样做,因为当人员中的数据存在时,用户仅被呈现为新视图,因此也是idList。因此,如果人员数组为空,则映射IdList并不是必需的。

1 个答案:

答案 0 :(得分:1)

您需要使用具有nil密钥路径的单个响应描述符。该响应描述符的映射是一个动态映射,它检查内容并返回一个处理两个子节的映射(所以你需要一个容器类)或者nil(放弃映射过程)。