2

Possible Duplicate:
Managing two NSURLConnection
NSURLConnection delegate

I have two different methods who calls different NSURLConnections;

Method A

NSURLConnection *serverConnection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfSideWebService]] delegate:self];

Method B

NSURLConnection *serverConnection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfMainWebService]] delegate:self];

The problem is, both of these NSURLConnections triggers same delegates;

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.serverData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    NSString *response = [[NSString alloc]initWithData:self.serverData encoding:NSUTF8StringEncoding];
    // use data
}

What I want to know that, can we tag NSURLConnections' or method called NSURLConnection, in order the identify which function or method so I can act upon it.

Can we set different delegates, or is there any other methodology that I should be following?

I hope I was clear, any other information can be supplied. Thanks.

Community
  • 1
  • 1
Bartu
  • 2,189
  • 2
  • 26
  • 50
  • possible duplicate of [NSURLConnection delegate](http://stackoverflow.com/questions/10886237/nsurlconnection-delegate), [Managing two NSURLConnections](http://stackoverflow.com/questions/7450806/managing-two-nsurlconnection), [Managing multiple NSURLConnections](http://stackoverflow.com/questions/332276/managing-multiple-asynchronous-nsurlconnection-connections) – jscs Jul 22 '12 at 20:36
  • first link has an complicated question, this is an easier definition of the problem for people who might have this issue, second one has broken links on the answer so you can not see the answer as well. Third one has the answer, but the answers below this question are unique and, IMO they are more easy both to understand and implement. Cheers. – Bartu Jul 22 '12 at 22:13
  • I don't see any links at all in the answers to the second question. – jscs Jul 22 '12 at 22:48
  • I flagged them, they are deleted now, as you can see there is no more accepted answer as well. Why are we arguing on this, this question might be asked more than once, but solutions are unique and solid. – Bartu Jul 23 '12 at 00:49
  • Your accepted answer here isn't unique -- it's exactly the same as the [most-upvoted answer](http://stackoverflow.com/a/7451143/603977) on the second link and [one of the answers](http://stackoverflow.com/a/4786710/603977) on the third. The first paragraph of the first link also mentions this solution. – jscs Jul 23 '12 at 03:16

3 Answers3

5

Why not set properties like:

NSURLConnection *serverConnection1;
NSURLConnection  *serverConnection2;

Then use like:

serverConnection1 = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfSideWebService]] delegate:self];

Then in your delegate methods check which connection it is:

if (connection == serverConnection1) {
    //Do stuff
} else if (connection == serverConnection2) {
    //Do other stuff
}
Rengers
  • 14,911
  • 1
  • 36
  • 54
Darren
  • 10,182
  • 20
  • 95
  • 162
  • Rengers, thanks for editing to display the code properly. I can never get it to display properly when using my phone to reply. – Darren Jul 22 '12 at 21:23
  • I ran into an issue with the method, but hopefully it won't affect other people. I had my first connection completion make the call to the second connection. When the second connection came back, I got very odd behaviour: alerts wouldn't show and the segue didn't perform, but I didn't get any errors. – Erik Allen Dec 05 '13 at 20:03
3

Your existing code is using 'self' as the delegate for both calls which is the root of your issue.

IMHO the best way to handle this is to create separate classes for each response handler. This helps to keep your code clean. Those classes should be NSURLConnectionDelegates that contain your response handling code.

When you create your NSURLConnection from the calling class make sure you set the correct delegate on creation.

Now each of your NSURLConnections will call back to their designated class when finished.

So your calling class might look something like this:

#import "FeedDownloader.h"
#import "URLConnectionResponseScenarioADelegate.h"

@implementation FeedDownloader

- (id)init {

    URLConnectionResponseScenarioADelegate *connectionDelegate = [[URLConnectionResponseScenarioADelegate alloc] init];
    NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.apple.com"]]  delegate:connectionDelegate];

    [myConnection start];
    return self;
}

@end

Your response handler class might look something like this:

#import "URLConnectionResponseScenarioADelegate.h"

@implementation URLConnectionResponseScenarioADelegate
@synthesize data;

#pragma mark NSURLConnection delegate methods

// The following are delegate methods for NSURLConnection. Similar to callback functions, this is how the connection object,
// which is working in the background, can asynchronously communicate back to its delegate on the thread from which it was
// started - in this case, the main thread.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    self.data = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
    [self.data appendData:_data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

}


@end
radesix
  • 5,834
  • 5
  • 24
  • 39
  • This might be the most sophisticated way to solve this issue, but it is a bit overwhelming, but this is the answer to my question as well, thanks a lot! – Bartu Jul 22 '12 at 22:10
1

Radesix suggested a nice method, but it may be a little overkill. Easy way to fix this is to store each NSURLConncection in a separate ivar and just check for pointer equality in your delegate methods.

self.serverConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if(connection == self.serverConnection) {
        //Do A
    } else {
        //Do B
    }
}
Rengers
  • 14,911
  • 1
  • 36
  • 54
  • 1
    This is a great option if you are only dealing with a couple of connections; however, if you are dealing with a long list of different API calls that might be made from the same calling class then this might cause some code clutter. All in all, a reasonable approach though. I've used it myself before! :) Also, by using the delegate approach you won't need to copy and paste your response handler code if you make the same API call from 2 or more classes. – radesix Jul 23 '12 at 14:32