Restkit mapping dependency

时光总嘲笑我的痴心妄想 提交于 2019-12-24 20:35:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!