I am trying to create something like a Network Manager using NSUrlConnections.
For that, I want to be able to send multiple requests, but I also want to be able to identify the client(delegate) that made the request when the response arrives.
I have created a NSDictionary like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:SERVER_TIMEOUT];
....
[clients setObject:client forKey:connection];
in “- (void)connectionDidFinishLoading:(NSURLConnection *)connection” I have something like this:
client = (id<RTANetworkDelegate>)[clients objectForKey:connection];
[clients removeObjectForKey:connection];
The Network Manager is the delegate for all the connections, I do some preprocessing and then I send the (parsed) response to the right delegate, that sent the request in the first place.
Unfortunately, it appears that a NSMutableURLRequest cannot be set as a key in a dictionary since it does not have the copyWithZone method and I get the error:
-[NSURLConnection copyWithZone:]:
unrecognized selector sent to
instance
Any help would be much appreciated!
Thanks!
=======================================
[Edit] I already found this in the meantime:
http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/
It seems to solve my problem.. I still don’t know if it’s the best solution though. I thought I would post it here since it might help others too.
You could use the
-hashvalue of the connection object as the key:I’d stay away from the actual URL or anything similar as two requests could potentially have the same URL.