Managing two NSURLConnection

2019-03-03 00:50发布

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?

3条回答
爷的心禁止访问
2楼-- · 2019-03-03 01:19
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if( [connection isEqual: AConnection] ){
        // do connection A stuff
    }
    else if( [connection isEqual: BConnection] ){
        // do connection B stuff
    }  
}
查看更多
疯言疯语
3楼-- · 2019-03-03 01:24

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

查看更多
老娘就宠你
4楼-- · 2019-03-03 01:30

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
});
查看更多
登录 后发表回答