I am intending to create 2 requests using NSURLConnection. When the server responds and calls connectionDidFinishLoading it passes in the connection as the parameter, but how do I identify which connection is passed in?
5 Answers
Save both NSURLConnection objects as member variables of whatever delegate object you passed to connectionWithRequest:delegate:. Then you can just compare each of those to the NSURLConnection passed to connectionDidFinishLoading:, and respond appropriately:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == firstConnection) {
// do something
}
else if (connection == secondConnection) {
// do something else
}
}
Another slightly more object-oriented option would be to create two different delegate objects, each of which knows how to deal with each each type of connection. Then just pass the appropriate delegate when you create each connection. That way you don't need to check to see which connection you have, because each delegate will only receive connectionDidFinishLoading: for its own connection.

- 17,818
- 4
- 49
- 65
-
3This is not a very scaleable solution – coneybeare Sep 13 '09 at 14:40
-
Not so scaleable, but nice solution, solved my problem. Thanks. – Krishna Feb 09 '12 at 14:28
-
@cduhn is it possible to subclass NSURLConnection and give it an ID property as an NSString for example. This way if you have an indefinite number of objects that, each, may need a connection (e.g accepting a list of friend requests) all you have to do is set your CustomNSURLconection's I.D to be the same value as the value that uniquely identifies your object e.g userID ? That way you dont have to have an instance of hundreds of NSURLconnections. I can not test this at the moment that is why I ask here – pnizzle Oct 31 '12 at 02:13
-
You can certainly subclass NSURLConnection and add a property to it. Another option is to add the NSURLConnections to a NSMutableDictionary, using something like a user ID (converted to NSNumber) as the key. I'm not sure what scenario you're referring to when you say, "an instance of hundreds of NSURLConnections", but it would be unusual for an app to require hundreds of simultaneously open connections. – cduhn Oct 31 '12 at 04:46
-
@cduhn please check this http://stackoverflow.com/questions/24075772/how-to-manage-multiple-asynchronous-nsurlconnection-request – Ranjit Jun 06 '14 at 06:45
I prefer different delegates for each connection, too. Although it's a bit of overhead. Fortunately, you can simplify things by using blocks. It's a new feature that doesn't exist in standard SDK yet, but there is 3rd-party framework called PLBlocks that you can use already. Here is an article on how to use them, it also contains example for NSURLConnection.
This is the client code making HTTP request with block callback:
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection sendAsynchronousRequest:req onCompletionDo: ^(NSData *data, NSURLResponse *res, NSError *err) {
NSLog(@"data: %ld bytes. res: %@, error: %@", (long)[data length], res, err);
[cell.activity stopAnimating];
}];

- 9,579
- 6
- 39
- 28
What I do in my projects is create a wrapper class for the connection. This way, you can keep a new instance for each connection you need, and maintain these classes in another manager class.
Something like [AsynchronousConnection initWithURL:delegate:selector:]
Then you can be ensure the right thing is called when the NSURLConnection is done/failed.

- 33,113
- 21
- 131
- 183
I used to create a custom wrapper around NSURLConnection
, too, but I've now switched over to ASIHTTPRequest. This is a fantastic library providing much more flexibility and features than NSURLConnection
. Have a look and give it a try, it's really worth it.

- 8,855
- 4
- 41
- 39
-
-
I used to use ASIHTTPRequest as well, but unfortunately from Sept 2011 it is no longer supported by its author: http://allseeing-i.com/%5Brequest_release%5D – Dan J Apr 16 '12 at 03:03
-
1@DanJ: A very popular alternative these days seems to be [AFNetworking](https://github.com/AFNetworking/AFNetworking/). – Daniel Rinser Jul 25 '12 at 16:17
-
Thanks. It's a shame that AFNetworking doesn't seem to have ARC support (even though ARC can be turned off for those files). – Dan J Jul 26 '12 at 15:54
-
Please do not refer https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
GO to the NSURLConnection.h file and you will find the following.
When created, an NSURLConnection performs a deep-copy of the NSURLRequest. This copy is available through the -originalRequest method. As the connection performs the load, this request may change as a result of protocol canonicalization or due to following redirects. -currentRequest can be used to retrieve this value.
Ultimately [connection currentRequest].URL absoluteURL might help.
Regards, PRASANNA.

- 41
- 1