1

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": "bob@foo.corp",
        "first_name": "Bob",
        "last_name": "Burroughs",
        "primary_phone": "16507001212"
    },
    {
        "user_id": "ddd8d8d8d8d",
        "email": "don@foo.corp",
        "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?

Rahul Jiresal
  • 1,006
  • 13
  • 24

2 Answers2

2

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

Mateusz
  • 1,222
  • 11
  • 22
  • The problem with that is, `accessTime` is not a property of `Contact`. Also, this approach will do it for each `Contact` object. I need it only once. – Rahul Jiresal Nov 20 '14 at 19:43
  • ok, I misunderstood your question. So NSUserDefaults is one of an option. Try this: http://stackoverflow.com/questions/14247540/access-response-data-in-success-callback – Mateusz Nov 20 '14 at 19:52
0

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.

Asciant
  • 2,130
  • 1
  • 15
  • 26
  • To me, this doesn't sound like a clean solution. This will create a new object of the ParentJSONObject every time I make the call. And then there will be multiple of such objects over time. I need to use the "access_time" over time, and I need the stored access time to be overwritten with the new one every time I get one, instead of having many of them. – Rahul Jiresal Nov 24 '14 at 17:00
  • No problems, if you have the ability to modify the JSON output in your API you could hard code a static [identification attribute](http://restkit.org/api/latest/Classes/RKEntityMapping.html#//api/name/identificationAttributes) so RK only creates a single parent object. Either way, good luck with your question! – Asciant Nov 24 '14 at 20:23