digitaljerry
digitaljerry

Reputation: 198

RestKit - Mapping multiple Entites from a (not really good RESTful) JSON response

My JSON response looks like this:

{
  "enabled": false,
  "number_of_articles": 20,
  "new_users": [
    {
        "id": "5001",
        "name": "Jimmy Valner"
    },
  ],
  "articles" : [
  { 
    "id" : 33122,
    "title" : "Something",
    "authors": [
      {
          "id": "511",
          "name": "Somebody"
      },
      {
          "id": "51",
          "name": "Somebody else"
      }
  }]
}

Not really a proper RESTful response I know. But I have to map it somehow to two different Entities (User and Article). A already have a working code for the articles part. It maps the Article and the Articles as a relation. I'm doing it like this:

RKMapping *mapping = [MappingProvider articlesMapping];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                            responseDescriptorWithMapping:mapping
                                            pathPattern:@"/articles/:userId/latest"
                                            keyPath:@"articles"
                                            statusCodes:statusCodeSet];
NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil
                                                  method:RKRequestMethodGET
                                                    path:[NSString stringWithFormat:@"/articles/%@/latest", [[CredentialStore sharedInstance] userId]]
                                              parameters:nil];

RKManagedObjectStore *store = [[DataModel sharedDataModel] objectStore];

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request
                                                                    responseDescriptors:@[responseDescriptor]];
operation.managedObjectCache = store.managedObjectCache;
operation.managedObjectContext = store.mainQueueManagedObjectContext;

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"%@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"ERROR: %@", error);
    NSLog(@"Response: %@", operation.HTTPRequestOperation.responseString);
}];
[operation start];

This is working as expected. I provide the mapping instructions in the + (RKMapping *)articlesMapping method. Even the authors part of the JSON response is mapped properly.

Now I want to map new_users array to a different entity (User obviously). How can I do that by not invoking another request to the server ? I had some success with creating another RKManagedObjectRequestOperation *operation but that of course triggers another network request to the server which I want to avoid. Should I look for the solution here in the RKObjectManager or should I somehow change the keyPath to start at root level and the do the mapping for different entities in the + (RKMapping *) method (probably not) ?

And another question .. how can I access the enabled, number_of_articles properties from this JSON? Again, I'm not dealing with a nice structured Restful response, but I have to deal with it somehow. I only want to access this properties somehow ... no mapping to entities needed.

Upvotes: 1

Views: 1565

Answers (1)

Wain
Wain

Reputation: 119041

RKManagedObjectRequestOperation has the method initWithRequest:responseDescriptors: for a reason. Note specifically the 's' on responseDescriptors and the fact that you're passing an array. This means you can configure multiple response descriptors, each with the same path pattern but different key paths and RestKit will process each of them on the response received from the server.

Note that when you do this you probably want to change your NSLog(@"%@", mappingResult.array); because it would contain all of the top level items in the list. Instead, you should probably use NSLog(@"%@", mappingResult.dictionary);

Upvotes: 4

Related Questions