Search code examples
iphoneobjective-cnsurlconnectionasihttprequest

Multiple Requests on ASIHTTPRequest


I need to download three different sets of data from three different URLs. I decided to use ASIHTTPRequest. Two of the URLs are JSON feeds which I need to parse and one of them is a .txt file online that I need to store locally.

Now the example that is on ASIHTTPRequest's website for an asynchronous request shows the following:

- (IBAction)grabURLInBackground:(id)sender {
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

To pass multiple URLs, I can call "request" on three different URLs. But I am not sure how I would handle them in the requestFinished method. The documentation shows it as:

- (void)requestFinished:(ASIHTTPRequest *)request {
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

How would this method differentiate between different requests, so that I can handle it differently?

Thank you,


Solution

  • You can differentiate between different requests by

    • setting the userInfo dictionary of the request
    • setting the didFinishSelector (and didFailSelector etc.) to different methods
    • using different classes as delegate
    • using blocks
    • using the request's tag property
    • subclass ASIHTTPRequest and override override requestFinished: and failWithError: (only recommended for complex situations)