Multiple NSURLConnection delegates in Objective-C

20,386

Solution 1

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.

Glad you're up and running!

Solution 2

Ben, while your info was helpful It didn't fully answer the question I asked.

I finally figured out how to setup my own delegates, which was what I was really asking.

I implemented it like so:

@interface DownloadDelegate : NSObject 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
@end

@implementation DownloadDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
@end

We use the delegate like so:

DownloadDelegate *dd = [DownloadDelegate alloc];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:dd];

Hope that helps anybody in the same position, and thanks again Ben for your help.

Thanks,

Brian Gianforcaro

Solution 3

I think the best way to handle multiple connections in a clean way is to keep a single delegate and just identify each NSURLConnection with a tag (it's a very VERY simple subclassing you can read about and copy from http://www.isignmeout.com/multiple-nsurlconnections-viewcontroller/ )

Basically to init every NSURLConnection with an identifying tag and then you can pull that tag in the delegate and using Switch-Case handle it according to whatever logic you need.

UPDATE

I've turned the subclassed NSURLConnection into a simple Category - a little simpler and cleaner

https://github.com/Shein/Categories

Solution 4

delegates are implemented as standard NSObject-descended objects.

You can point both connections to the same delegate.

The delegate should implement the NSURLConnectionDelegate methods you'd like to catch (such as -connection:didReceiveData: and -connectionDidFinishLoading:). These methods will get called by the delegate as appropriate.

Solution 5

Try my MultipleDownload class at http://github.com/leonho/iphone-libs/tree/master, which it handles multiple NSURLConnection objects for you.

Share:
20,386
Brian Gianforcaro
Author by

Brian Gianforcaro

Updated on May 25, 2020

Comments

  • Brian Gianforcaro
    Brian Gianforcaro almost 4 years

    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.

    Right now I'm using:

    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.

    Thanks, Brian Gianforcaro