Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between NSURLConnection objects in delegate

I have two request starting one after the other. Starting request like this

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection * connection = [[NSURLConnection alloc]
                                initWithRequest:request
                                delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
[connection start];

and another request starting like this.

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.apple.com"]];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
[connection1 release];

How can i differentiate between these two in delegate method?

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{}

Don't want to keep any extra class variable for this purpose.

like image 675
NaXir Avatar asked May 06 '13 07:05

NaXir


1 Answers

It's Simple :

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     if (connection == connection1)
     {
         //It's for connection1.
     }
     else if (connection == connection2)
     {
         //It's for connection2.
     }
}

You can go through this Beautiful SO Question : Managing multiple asynchronous NSURLConnection connections

like image 151
Bhavin Avatar answered Sep 22 '22 11:09

Bhavin