Restkit 映射依赖 [英] Restkit mapping dependency

查看:58
本文介绍了Restkit 映射依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否让restkit中的映射相互依赖.我有以下 JSON 结构:

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

我有 2 个响应描述符,以便深入到数组 IdList &人.

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

我的映射代码:

+ (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 数组.我想这样做是因为用户只显示一个新视图,当 Persons 中的数据存在时,因此还有 idList.因此,如果person 数组为空,则映射IdList 就不是必需的.

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.

推荐答案

您需要使用带有 nil 键路径的单个响应描述符.该响应描述符的映射将是一个动态映射,它检查内容并返回处理两个子部分的映射(因此您需要一个容器类)或 nil(放弃映射过程).

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).

这篇关于Restkit 映射依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆