0

I have two NSURLRequest objects, connecting to a web service and invoking 2 different services.

The problem is that I have a random results, sometimes the first one is displayed first and sometimes the second NSURLRequest is the first.

NSString *urla=@"http://localhost:8080/stmanagement/management/retrieve_dataA/?match_link=";
NSString *uria = [urla stringByAppendingString:self.lien_match];
NSURL *urlla= [ NSURL URLWithString:uria];
NSURLRequest *requesta =[ NSURLRequest requestWithURL:urlla];

NSString *urlb=@"http://localhost:8080/stmanagement/management/retrieve_dataB/?match_link=";
NSString *urib = [urlb stringByAppendingString:self.lien_match];
NSURL *urllb= [ NSURL URLWithString:urib];
NSURLRequest *requestb =[ NSURLRequest requestWithURL:urllb];

connectiona=[NSURLConnection connectionWithRequest:requesta delegate:self];
connectionb=[NSURLConnection connectionWithRequest:requestb delegate:self];

if (connectiona){
    webDataa=[[NSMutableData alloc]init];
}

if (connectionb){
    webDatab=[[NSMutableData alloc]init];
}

Is that correct what I'm doing? Should I add a small break between the two NSURLRequests?

Because at every view execution I have a random result. (I'm setting the results to two UITableView objects).

Nate
  • 31,017
  • 13
  • 83
  • 207
androniennn
  • 3,117
  • 11
  • 50
  • 107
  • 1
    @glektrik your posted topic is since 2008! There is absolutely some things that has changed.... – androniennn Mar 14 '13 at 01:23
  • Is your problem that you're getting back results from both requests, possibly from the second one (connectionb) first, and you can't figure out which table view to put them in? – Nate Mar 14 '13 at 01:40
  • @androniennn things have changed indeed - BUT - correct answers usually remain valid. – Till Mar 14 '13 at 02:10
  • Incidentally, I don't know that this should be considered a duplicate, as the candidate dup is about an arbitrary number of concurrent requests, and the solutions are all more complicated than is really needed if you just have **two**. – Nate Mar 14 '13 at 02:10
  • @Nate check the answers - there is one that specifically handles two. – Till Mar 14 '13 at 02:11
  • @Till, yep. Good catch. I'd still say (as I missed that answer myself) that this two-connections-only question is worthy of leaving open. I also don't think [that answer](http://stackoverflow.com/a/4786710/119114) has a particularly detailed response. – Nate Mar 14 '13 at 02:15

1 Answers1

2

I think your "problem" is that self is the connection delegate for both of your connections. These type of connections are asynchronous, so there's no guarantee that A will complete before B. Your code should handle whatever order the web server returns data in.

I suppose you could make the two methods synchronous (don't start B until A completes), but I don't think there's really any need to do that.

The good news is that the NSURLConnectionDelegate callbacks pass you the NSURLConnection object, so you can use that to determine whether you're getting a response to A or B. That information should tell you whether to put the data in the A or B web data object, and whether to update table view A or B, when the request completes. For example:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // determine which request/connection this is for
    if (connection == connectiona) {
        [webDataa appendData: data];
    } else if (connection == connectionb) {
        [webDatab appendData: data];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // determine which request/connection this is for
    if (connection == connectiona) {
        NSLog(@"Succeeded! Received %d bytes of data",[webDataa length]); 
        // TODO(?): update the data source for UITableView (A) and call:
        [tableViewA reloadData];  
    } else if (connection == connectionb) {
        NSLog(@"Succeeded! Received %d bytes of data",[webDatab length]);   
        // TODO(?): update the data source for UITableView (B) and call:
        [tableViewB reloadData];  
    }

    // release the connection* and webData* objects if not using ARC,
    //  otherwise probably just set them to nil
}

This solution requires you keeping connectiona and connectionb as persistent ivars, not local variables in the code you posted. It looks like you're probably doing that, but since you don't show their declaration, I just wanted to be sure.

You should also implement the other delegate callbacks, of course, but the above two should give you a good example of the general solution.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • You're the best! Thank you very very much! you've made my day :)). Two days with that problem :/. Thank youuu... :))) – androniennn Mar 14 '13 at 11:33