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?
How about assigning tags to each connection and checking the tags via an if/else or switch in the connectionDidFinishLoading?
Use GCD with sendSynchronousRequest: requests, they will be run in the background.
Example: