1

I want to do two async request from two different kml file, so I setup two requests first:

NSString *server1URL = [NSString stringWithFormat:...];
NSMutableURLRequest *firstRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:server1URL]];
[firstRequest setHTTPMethod:@"GET"];
NSURLConnection *AConnection = [NSURLConnection connectionWithRequest:firstRequest delegate:self];

NSString *server2URL = [NSString stringWithFormat:...];
NSMutableURLRequest *secondRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:server2URL]];
[secondRequest setHTTPMethod:@"GET"];
NSURLConnection *BConnection = [NSURLConnection connectionWithRequest:secondRequest delegate:self];

Then I init NSMutableData I will be using:

AResponseData = [[NSMutableData alloc] init];
BResponseData = [[NSMutableData alloc] init];

Then, I refer this post and did this:

connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(connectionToInfoMapping, AConnection, [NSMutableDictionary dictionaryWithObject:AResponseData forKey:@"receivedData"]);
CFDictionaryAddValue(connectionToInfoMapping, BConnection, [NSMutableDictionary dictionaryWithObject:BResponseData forKey:@"receivedData"]);

OK, then there're delegates:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
    [[connectionInfo objectForKey:@"receivedData"] appendData:data];
}

So with this I can get data append to the correct NSMutableData that matches the connection.

Now in - (void)connectionDidFinishLoading:(NSURLConnection *)connection, I want to "If A finish, do this, if B finish, do this", and my question is, how can I do this?

Community
  • 1
  • 1
Derek Li
  • 3,089
  • 2
  • 25
  • 38
  • I would suggest subclassing NSURLConnection and adding an item like a key or a tag to identify each request with and using that to override the init method where you initialize the request. This is what I currently do in my app where I need multiple async connections. I used [this](http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/) and [this](http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/) to solve the issue in my app. – Bourne Sep 16 '11 at 22:10

3 Answers3

2
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if( [connection isEqual: AConnection] ){
        // do connection A stuff
    }
    else if( [connection isEqual: BConnection] ){
        // do connection B stuff
    }  
}
Henry Cooke
  • 2,563
  • 2
  • 23
  • 22
  • I ran into an issue when I tried this method to make the calls serially. 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. Oddly, I didn't get any errors. – Erik Allen Dec 05 '13 at 20:07
0

Use GCD with sendSynchronousRequest: requests, they will be run in the background.

Example:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{
    NSURLRequest *request = [NSURLRequest requestWithURL:url1];
    NSURLResponse *response;
    NSError *error;
    NSData *data1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // do something with the data
});

dispatch_async(queue, ^{
    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSURLResponse *response;
    NSError *error;
    NSData *data2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // do something with the data
});
zaph
  • 111,848
  • 21
  • 189
  • 228
-1

How about assigning tags to each connection and checking the tags via an if/else or switch in the connectionDidFinishLoading?

Arsalan Habib
  • 1,395
  • 14
  • 20
  • How can I assign "tag" to connection? Do I need to subclass `NSURLConnection`? Thanks. – Derek Li Sep 16 '11 at 22:11
  • yes a simple custom class that adds a tag to your NSURLConnection. That should do the trick. EDIT: Towards that end you can store the actual url in that custom class too so that u don't have to deal with custom tags and just check the url itself in the connectionDidFinishLoading. – Arsalan Habib Sep 16 '11 at 22:15