Home > DeveloperSection > Forums > Multiple NSURLConnection delegates in Objective-C
Tarun Kumar

Total Post:214

Posted on    August-27-2015 12:36 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 826  View(s)
Rate this:

I have two NSURLConnections. The second one depends on the content of the first, so handling the data received from the connection will be different for the two connections.

I'm just picking up Objective-C and I would like to know what the proper way to implement the delegates is.

NSURL *url=[NSURL URLWithString:feedURL];
NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:urlR delegate:self];

I don't want to use self as the delegate, how do I define two connections with different delegates?

NSURLConnection *c1 = [[NSURLConnection alloc] initWithRequest:url delegate:handle1];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:handle2];

How would do i create handle1 and handle2 as implementations? Or interfaces? I don't really get how you would do this.

Any help would be awesome.

Glen Martin
Glen Martin

Total Post:27

Posted on    August-30-2015 11:34 PM

In your sample, you alloc a DownloadDelegate object without ever init'ing it.
DownloadDelegate *dd = [DownloadDelegate alloc];

This is dangerous. Instead:
DownloadDelegate *dd = [[DownloadDelegate alloc] init];

Also, it's not strictly necessary to declare your delegate response methods in your @interface declaration (though it won't hurt, of course). Finally, you'll want to make sure that you implement connection:didFailWithError: and connectionDidFinishLoading: to -release your DownloadDelegate object, otherwise you'll leak.

Don't want to miss updates? Please click the below button!

Follow MindStick