views:

8256

answers:

6

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

+3  A: 

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.

Ben Gottlieb
+3  A: 

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

Brian Gianforcaro
+2  A: 

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!

Ben Gottlieb
+1  A: 

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

leonho
A: 

since the delegates are called asynchronously, they could call didfinishloading in random order. you can then use a state check to determine if the "other" download is done yet before continuing.

i use 2 delegates:

for instance (this is pseudo oc):

jsondelegate = [[JSonDelegate alloc]initWithCaller:self andSelector:@selector(jsonDone:)]
otherdelegate = [[OtherDelegate] initWithCaller:self andSelector:@selector(otherDone:)]

when each delegate finishes, the delegate informs the caller, by calling the 2 done methods.

each done method receives the url data, and saves its state to an ivar. then they check to see if the other ivar is set, and continue processing if they both are done.

if(self.jsonString && self.otherData){
  continueProcessing
}

hope this helps.

McClain Looney
A: 

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.

shein