Response Descriptor for RestKit JSON Metadata

367 views Asked by At

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?

2

There are 2 answers

2
Mateusz On BEST ANSWER

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

2
Asciant On

You could create a relationship between the parent JSON object (that contains "access_time") and the child "profiles" objects with RKRelationshipMapping.

Then instead of having your response descriptor directly accessing the keyPath:@"profiles", you can set it to keyPath:nil and access the whole JSON object including access_time and associated profiles.

You would also need to ensure you had a corresponding Entity and Relationships (in the datamodel) for the parent JSON object (you can call it whatever you like). Then back in the file with the RestKit mappings add the relationships to the parent mapping object with addPropertyMappingsFromArray:.

Then once the request is returned you can iterate through the associated profile objects of the parent JSON (assuming you have XCode create the associated NSManagedObject subclasses) with a simple:

// allObjects returns an NSArray representation of the NSSet
NSArray *profiles = [[parentObject valueForKeyPath:@"profiles"] allObjects];

Hopefully this helps.