Search code examples
iosobjective-cjsonrestkitrestkit-0.20

Response Descriptor for RestKit JSON Metadata


I have a JSON response that returns me a list of objects, and also a timestamp value as "MetaData". The response looks something like this --

{
  "access_time": 1416467865510,
  "profiles" : [
    {
        "user_id": "bbb91ae431b",
        "email": "[email protected]",
        "first_name": "Bob",
        "last_name": "Burroughs",
        "primary_phone": "16507001212"
    },
    {
        "user_id": "ddd8d8d8d8d",
        "email": "[email protected]",
        "first_name": "Don",
        "last_name": "Darko",
        "primary_phone": "14154001212"
    }
  ]
}

My RestKit descriptor code looks something like this. And this is working well, I am getting all objects.

RKEntityMapping *contactMapping = [RKEntityMapping mappingForEntityForName:@"Contact" inManagedObjectStore: managedObjectStore];
[contactMapping addAttributeMappingsFromDictionary:@{
                                                   @"user_id" : @"userId",
                                                   @"email" : @"email",
                                                   @"first_name" : @"firstName",
                                                   @"last_name" : @"lastName",
                                                   @"primary_phone" : @"primaryPhone"
                                                   }];
contactMapping.identificationAttributes = @[@"userId"];
RKResponseDescriptor *contactResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:contactMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"profiles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

The above thing works well for me. However, I wanted to have access to the access_time field above too. How do I get access to that? I am thinking of storing that value in NSUserDefaults for later use since it is not a field that is a part of the User/Contact object. How do I do it?


Solution

  • try this:

    [contactMapping addAttributeMappingsFromDictionary:@{
                                                   @"user_id" : @"userId",
                                                   @"email" : @"email",
                                                   @"first_name" : @"firstName",
                                                   @"last_name" : @"lastName",
                                                   @"primary_phone" : @"primaryPhone",
                                                   @"@parent.access_time" : @"accessTime",
                                                   }];
    

    You can read more here