0

After my last question was solved the JSON I'm receiving from the server changed to the following and I'm stuck handling the mapping to save the data with Core Data.

Entity

 Key 
 - alias 
 - key
 - keyType
 - keyword
 - unid
 - until

JSON (from Server)

{
    "documents": 1026,
    "configuration": 
    {
        ...
    },
    "data": 
    [
        {
            "alias": "",
            "key": "SALUTATION",
            "keyType": "S",
            "keyword": "Mr",
            "unid": ""
        },
        ...
        {
            "alias": "Automobile",
            "key": "ACCOUNT_MARKET_SEGMENT",
            "keyType": "A",
            "keyword": "Automobile",
            "unid": ""
        }
    ],
    "documentsFound": 770,
    "maxCount": -1,
    "since": "20120326200001",
    "until": "20120326211309"
}

Now I want to map all the data from "data" plus the key "until" for the entity "Key" but can't find the right solution. My mapping so far to get the data looks like this and works well but misses the "until"-key, of course.

RKManagedObjectMapping* keyMapping = [RKManagedObjectMapping mappingForClass:[Key class]];
keyMapping.rootKeyPath = @"data";
[keyMapping mapKeyPath:@"key" toAttribute:@"key"];
[keyMapping mapKeyPath:@"keyword" toAttribute:@"keywordEN"];
[keyMapping mapKeyPath:@"alias" toAttribute:@"alias"];
keyMapping.setDefaultValueForMissingAttributes = YES;

Thanks for your ideas!

Community
  • 1
  • 1
flashfabrixx
  • 1,183
  • 9
  • 22

1 Answers1

2

You are probably going to want to do two mappings. The first mapping will enclose the entire object and will have a relationship to the nested 'data' path.

RKObjectMapping *keyMapping = [RKObjectMapping mappingForClass:[Key class]];
[keyMapping mapAttributes:@"alias", @"key", nil];
[keyMapping mapKeyPath:@"keyword" toAttribute:@"keywordEN"];

RKObjectMapping *outerMapping = [RKObjectMapping mappingForClass:[Container class]];
[outerMapping mapKeyPath:@"data" toRelationship:@"keys" withMapping:keyMapping];
[outerMapping mapAttributes:@"since", @"until", "maxCount", "documentsFound", nil];

That will give you a new object with your metadata and then an array of the key objects on the keys attribute of your container. Rather than using the rootKeyPath, you can use the resourcePath based mapping registration on the 0.9.4 development branch (about to be released).

Blake Watters
  • 6,607
  • 1
  • 43
  • 33