iosobjective-cafnetworking

AFNetworking nested param request


I have a Rails API that generates locations, and menus for those locations. In my iOS app, I'm using AFNetworking to retrieve a JSON list of those locations in a table view.

This is the AFNetworking request I use to get Location data:

LocationTableViewController.m

[[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil
    success:^(AFHTTPRequestOperation *operation, id response) {
        NSLog(@"Response: %@", response);
        NSMutableArray *results = [NSMutableArray array];
        for (id locationDictionary in response) {
            Location *location = [[Location alloc] initWithDictionary:locationDictionary];
            [results addObject:location];
            
        }
        self.results = results;
        [self.tableView reloadData];
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error fetching locations!");
        NSLog(@"%@", error);
        
    }];

A sample of the JSON returned for the location request:

 {
    "created_at" = "2013-02-23T19:25:26Z";
    id = 2;
    lat = "48.870175";
    long = "2.779899999999998";
    name = "Disneyland Hotel";
    "places_id" = fd90769e2cf40d6cb6cb1a3d0bfd0ce5d37ed331;
    "street_address" = "Rue de la Marni\U00e8re, Chessy, France";
    "updated_at" = "2013-02-23T19:25:26Z";
}, 

The name and address are displayed:

enter image description here

When a user taps on a location cell, I would like to display a table view of that locations beer list. The Rails APPI route (for location_id => 1) is http://localhost:3000/locations/1/beers.json and outputs the following JSON for a beer that belongs to location_id:1:

{
"created_at":"2013-02-23T19:25:53Z",
"description":"fkjgvjkg",
"id":1,"location_id":1,
"name":"nhoihioh",
"price":"2.5","style":"ipa",
"updated_at":"2013-02-23T19:25:53Z"
}

When the user taps a location cell I want to display the beer list for that location, which is consumed via http://localhost:3000/locations/(whatever location_id is associated with the table cell that was tapped)/beers.json

How can I insert the location_id into the following AFNetworking request that occurs in the next table view when a user taps a location cell?

BeerTableViewController.m

[[LocationApiClient sharedInstance] getPath:@"locations/**(location_id from location table cell inserted here)**/beers.json" parameters:nil
    success:^(AFHTTPRequestOperation *operation, id response) {
        NSLog(@"Response: %@", response);
        NSMutableArray *results = [NSMutableArray array];
        for (id beerDictionary in response) {
            Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary];
            [results addObject:beer];
            
        }
        self.results = results;
        [self.tableView reloadData];
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error fetching beers!");
        NSLog(@"%@", error);
        
    }];

I assume I have to log the location_id and pass that value as a param of the URL when a table cell is tapped, but am not sure how to do that.


Solution

  • As you are working with JSON you should probably be using AFJSONRequestOperation as it will parse the response data for into a dictionary.

    AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, id locationJSON) {
    
    ....
    
    };
    

    You can then get the places_id from the dictionary and do another call to get the beer info.