Search code examples
iosobjective-cjsonrestkit

Multi root with a GET request


I am searching a way to map 2 different arrays with the following JSON

"establishments": [
    {
        "id": 1,
        "name": "Boudin Sourdough Bakery and Cafe",
        "address": "1 Stockton St",
        "zip_code": "94108",
        "city": "San Francisco",
        "country": "us",
        "phone_number": "",
        "position": {
            "latitude": 37.78590500000001,
            "longitude": -122.406289
        },
        "distance_from": 13.75783097391759
    }
],
"places": [
    {
        "id": 3,
        "name": "Private Place",
        "position": {
            "latitude": 37.78583400000001,
            "longitude": -122.406417
        },
        "is_private": false,
        "distance_from": 0
    }
]

I have 2 different mappings. One for establishment and another for place. I found some help with the documentation but it's working only for PUT or POST requests.

Currently, I have the following request

[session.objectManager getObjectsAtPathForRouteNamed:APICallSearchSearchPlaceRouteName object:nil parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSArray * resultPlaces=mappingResult.array;

    [delegate APICallDidSearch:resultPlaces];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSError* myError=[self catchCustomErrorInRKError:error setDomain:@"Search"];
    if (myError.code == NSURLErrorCancelled) return;

    [delegate APICallDidFailSearch:myError];
}];

Mapping & Mapping Descriptor

RKEntityMapping *establishmentMapping = [APICallEstablishment RKGetEstablishmentMappingForManagedObjectStore:self.appDelegate.managedObjectStore];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:establishmentMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:APICallSearchSearchPlacePattern
                                                                                       keyPath:@"establishments"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[session.objectManager addResponseDescriptor:responseDescriptor];

I tried to adapt the example to my case with that (just before my request written above)

RKManagedObjectRequestOperation *operation = [session.objectManager appropriateObjectRequestOperationWithObject:establishmentMapping method:RKRequestMethodGET path:@"/whatever" parameters:nil];
operation.targetObject = nil;
//Does it still exist ? (XCode says an error)
//operation.targetObjectID = nil;
[session.objectManager enqueueObjectRequestOperation:operation];

Thank you in advance for your help.


Solution

  • Using session.objectManager getObjectsAtPathForRouteNamed... is a good option (better than trying to use RKManagedObjectRequestOperation directly).

    You need to create another response descriptor, like responseDescriptor, but for places key path and mapping.