3

Whenever I do a curl call using the below code:

NSURL *url = [NSURL URLWithString:requestURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
timeoutInterval:30];
if (connectionInProgress) {
    [connectionInProgress cancel];
}
connectionInProgress = [[NSURLConnection alloc]initWithRequest:request delegate:self     startImmediately:YES];

connectionDidFinishLoading is my final destination where I can manipulate the response data and call my next methods to continue with the app . If I hard-code some specific tasks like

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[someLabel setText:parsedTextFromXMLData];
}

If I need to do another curl call to a different address, wouldn't someLabel setText always get re-set again? Is there a way to make this delegate function behave differently on each curl call? (btw, is connectionDidFinishLoading usually the right place to put the next step of codes?) If so then wouldn't it always get called again by the next curl call?

codingbadger
  • 42,678
  • 13
  • 95
  • 110
sonoluminescence
  • 1,002
  • 1
  • 11
  • 28

2 Answers2

1

There're 2 options to do this:

  1. you can implement a separate class, that will be responsible for handling NSURLConnection delegate stuff and create a separate instance for each request
  2. you can use NSObject key-value methods on NSURLConnection instance for setting up some tag, that will be checked in connectionDidFinishLoading: method

For me, option 1 will be a better approach

Denis
  • 6,313
  • 1
  • 28
  • 27
1

Have a look at this S.O. post for a recipe concerning NSURLConnection and multiple requests.The suggestion is doing something like this:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  if (connection == firstConnection) {
    // do something
   }
   else if (connection == secondConnection) {
    // do something else
   }
 } 

EDIT: the idea here is that connectionDidFinishLoading is a method of your own delegate (so you write it). In the delegate, you store the address of each connection you create; then, when the connection comes back with the data, you tell which connection it is by comparing its address to the one you stored in the delegate. -END EDIT

Another option you have is using the ASIHTTPRequest framework, which offers a request-based (as opposed to connection-based) delegation mechanism, so each request has got a delegate object to handle the result; or, in other words, the delegate receives a reference to the request, so you can easily tell which request result you are handling.

ASIHTTPRequest offers a bunch of advantages over NSURLConnection. You can read about them in this S.O. post.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Thanks for the quick response and the post. Btw, how do I distinguish the between 1st and 2nd connection? Is there a connection title/identifier that I can check? Thanks – sonoluminescence Nov 14 '11 at 12:24
  • please, see my edit, hope it helps. anyway, there is no connection identifier, that is why you use the address. – sergio Nov 14 '11 at 12:32