Question

I have the following 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”: []
        }]
    ]}

I get this my using a RKResponseDescriptor like so:

 RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider validationMapping] method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [self addResponseDescriptor:responseDescriptor];

My validation mapping contains the following:

    + (RKDynamicMapping *)validationMapping {

        RKDynamicMapping * dynamicMapping = [RKDynamicMapping new];

        [dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

            NSArray *personsAray = [representation objectForKey:@"Persons"];

            if (!personArray || !personArray.count) {
                //Fail operation no contents 
                return nil;


            }else {

                NSArray *idsArray = [representation objectForKey:@"IdList"];

                if (!idsArray || !idsArray.count) {
                     //Fail operation no contents 
                    return nil;

                }else {

                    if (idsArray) {

                        [[NSUserDefaults standardUserDefaults] setObject:idsArray forKey:kIds];
                    }

                    [[NSUserDefaults standardUserDefaults] synchronize];

                }

               NSArray *categoryArray = [menuArray valueForKey:@"Items"];

                    if (!categoryArray || !categoryArray.count) {

                        return nil;
                    }else {

                        // If we have some data map the response!
                        return [self theMapping];

                    }

            }

            return nil;

        }];

        return dynamicMapping;



    }

    + (RKEntityMapping *)theMapping {
        RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Person" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]];

// I want to start the mapping from Person, how do I do that?
        [mapping mappingForSourceKeyPath:@"Persons"];


        mapping.assignsNilForMissingRelationships = YES;
        mapping.assignsDefaultValueForMissingAttributes = YES;

        [mapping addAttributeMappingsFromDictionary:@{
                                                      @"Id": @"remoteID",
                                                      @"Name": @"name",
                                                      }];


        mapping.identificationAttributes = @[ @"remoteID" ];

        [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items"
                                                                                       toKeyPath:@"misc"
                                                                                     withMapping:[MappingProvider productMapping]]];




        return mapping;
    }

How do I tell restKit to start the mapping from the Persons keypath in my "theMapping" method? I have tried using [mapping mappingForSourceKeyPath:@"Persons"]; but without luck.

Was it helpful?

Solution

You would generally use 2 response descriptors in this situation.

Your first response descriptor would use the dynamic mapping with a key path of IdList. It is a bit of an abuse and stores the representation to user defaults and returns nil.

Your second response descriptor would use the normal mapping with a key path of Items (by the looks of it, though your code uses undefined variables) and rely on RestKit to not do any mapping if there are no results (though you would need to use another dynamic mapping in the case where you had a fetch request block for deletion).

mappingForSourceKeyPath: returns a property mapping on the receiver with the specified key path, so is completely unrelated to what you're trying to do. It's the key path on the response descriptor that drills into the data structure. You can drill in with the key paths in the mappings, but that drills into objects, not dictionaries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top