MULTIPLE NSURLCONNECTION DELEGATES IN OBJECTIVE-C

Tarun Kumar

Total Post:214

Points:1642
Posted by  Tarun Kumar
 884  View(s)
Ratings:
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.

  1. Glen Martin

    Post:27

    Points:191
    Re: Multiple NSURLConnection delegates in Objective-C

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!