Search code examples
iosxmlnsmutableurlrequest

Multiple iOS GET Requests (Dynamic Number of Requests)


I am using an online API to retrieve random famous quotes. In my code, I use:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urloftheapi]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
    [request setHTTPMethod:@"GET"];


    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    [connection release];

However, I want it to pull in possibly more than one quote. The API doesn't include a function for multiple quotes, so I would need to run more than one NSMutableURLRequest to get it all. However, since it could be 1 request, and could be several, I'm not sure the best way to implement this in code. Thoughts?


Solution

  • Depending on how the API is setup you could do one request to get the list of the quotes (IE an array / dictionary of quoteID's, etc). From there you can loop through all the quoteID's you got and do a specific request to get each quote by the quotes ID property.

    Without a description of the API its nearly impossible to give you an exact answer but what I just described is the general approach for these types of problems.

    1) Make a request to get the list of quotes (just their primary key / unique id's)

    2) Loop through all the id's from the previous request and make a specific request to get the quote by what id.

    Using the AFNetworking Library it would look something like this. Keep in mind the requests are processed in a different thread so you may want to setup a NSNotification to keep track of when you finished getting each individual quote and/or keep track of the total number of quotes (the outside loop) and the current individual quote you are requesting (the inside loop). By comparing these two counts you can tell when you are finished.

    NSMutableArray *allQuotes = [[NSMutableArray alloc] init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *url = @"www.whatever.com/api/quotes";
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *quoteList = (NSArray *)responseObject;
        for(NSString *quoteID in quoteList)
        {
            [manager GET:url parameters:@{@"quoteID":quoteID} success:^(AFHTTPRequestOperation *operation, id responseObject) {
                [allQuotes addObject:responseObject];
                [NSNotificationCenter defaultCenter] postNotificationName:@"FINISHED_GETTING_QUOTE" object:allQuotes];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                //Handle failure here...
            }];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //Handle failure here...
    }];
    

    -(void)finishedGettingQuotes:(NSNotification *)notification
    {
       if(currentRequest == totalRequests)
       {
          NSMutableArray *temp = notification.object;
       }
    }