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?

1
  • 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 and this to solve the issue in my app.
    – Bourne
    Sep 16, 2011 at 22:10

3 Answers 3

2
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if( [connection isEqual: AConnection] ){
        // do connection A stuff
    }
    else if( [connection isEqual: BConnection] ){
        // do connection B stuff
    }  
}
1
  • 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 5, 2013 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
});
-1

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

2
  • How can I assign "tag" to connection? Do I need to subclass NSURLConnection? Thanks.
    – Derek Li
    Sep 16, 2011 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. Sep 16, 2011 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.