views:

247

answers:

4

I have a class that makes multiple asynchronous connections where each connection performs its own logic in the delegate methods.

Because the delegate is the class itself, how can I implement this separation in logic in the NSURLConnection delegate methods?

+1  A: 

Either check the passed in NSURLConnection against a saved value to see which connection is responsible; or make the delegate different objects; or make the callback behave in a generic manner.

Paul Lynch
If the delegate and the calling class must be the same then you need to store references to each connection and test for them in `- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response`
ctshryock
+2  A: 

Sounds to me like you need to better represent your objects in terms of object orientation.

You should have one class that manages multiple classes that each manage their own URL connection.

Jasarien
+3  A: 

My vote is for creating a different class for each operation you're doing. It may sound like a lot more work, but your code is going to be a heck of a lot cleaner which will probably lead to less bugs.

bpapa
A: 

I ran into this problem like this. I have a class that does the same thing. I worked around it by storing each NSURLConnection object in a mutable dictionary instance var with its hash as the key. I then added a cancelAllConnections method in the class and I call it in each view controller's viewDidUnload method. The method removes all the connection objects in the mutable dictionary. Then I added a check in NSURLConnection's connectionDidFinishLoading to check for the hash key in the mutable dictionary. If the hash value doesn't exist, that means the connection was canceled, and the the callback selector won't be performed on a garbage object.

Calvin L