I’m using RestKit to pull data from the Foursquare API into my iphone app, but having trouble with the followup API call for a nested object.
Specifically:
-
I call the Venues search API (https://developer.foursquare.com/docs/venues/search) to retrieve a list of Venues. Each Venue has a unique ID which is included in the response. I do this with the following in my ViewController:
[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self]; -
I then do the RestKit mapping etc and store the Venues in a data array. Everything working fine up to here.
-
At this point I loop through the Venues in the data array, and have to make a followup API call to retrieve more details about each Venue. For each Venue, I use the unique ID and call the Venue detail API (https://developer.foursquare.com/docs/venues/venues). This is the part that stumps me. I am trying to get an NSArray of Photo objects returned from this second API call. So far I have tried variations of this:
for (id venue in self.data){ Venue *myvenue = (Venue *)venue; RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[NSString stringWithFormat:@"/venues/%@", myvenue.venueid] queryParameters:queryParams]; [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self]; }and in my mapping:
RKObjectMapping *photosMapping = [RKObjectMapping mappingForClass:[Photo class]]; [photosMapping mapKeyPathsToAttributes:@"url", @"imageURL", nil]; [venueMapping mapRelationship:@"photos" withMapping:photosMapping]; [objectManager.mappingProvider setMapping:photosMapping forKeyPath:@"response.photos"]; // not sure if this keypath is for the second API calland my Venue class has this:
@property (strong, nonatomic) NSArray *photos;
Venue.photos always returns empty. Any suggestions?
Not sure if this is the ‘best’ way to do it, but I got it working in the end. Paul de Lange’s comment about not mapping the venue and the photos correctly set me off on the correct path.
The foursquare response was not in the format that I required in my app, so I had to use RKObjectLoader’s willMapData to modify the response slightly before the rest of the mapping operation.
Here it is in case it helps anybody in the future:
And in my mapping: