管理两个NSURLConnection的

问题描述:

我想做两个不同的KML文件中的两个异步请求,所以我设置两个要求第一:管理两个NSURLConnection的

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]; 

然后我初始化NSMutableData我将使用:

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

然后,我指this后,并做到这一点:

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

好的,那么有代表:

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

因此,我可以将数据追加到匹配连接的正确NSMutableData。

现在在- (void)connectionDidFinishLoading:(NSURLConnection *)connection,我想要“如果完成,做到这一点,如果B完成,做到这一点”,我的问题是,我该怎么做?

+0

我建议子类NSURLConnection的和添加的项目像一个键或一个标签,以识别与每个请求,并使用该覆盖其中在初始化请求init方法。这是我目前在我的应用程序中所做的,我需要多个异步连接。我使用[this](http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/)和[this](http://blog.emmerinc。 be/index.php/2009/03/15/multiple-async-nsurlconnections-example /)来解决我的应用程序中的问题。 – Bourne

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    if([connection isEqual: AConnection]){ 
     // do connection A stuff 
    } 
    else if([connection isEqual: BConnection]){ 
     // do connection B stuff 
    } 
} 
+0

当我尝试使用这种方法来串行调用时遇到了问题。我有我的第一次连接完成拨打第二个连接。当第二个连接回来时,我得到了非常奇怪的行为:警报不会显示,并且segue没有执行。奇怪的是,我没有得到任何错误。 –

如何为每个连接分配标记并通过connectionDidFinishLoading中的if/else或switch来检查标记?

+0

如何为连接分配“标签”?我需要继承'NSURLConnection'吗?谢谢。 –

+0

是一个简单的自定义类,它为您的NSURLConnection添加一个标签。这应该够了吧。编辑:为此,您可以将实际的网址存储在该自定义类中,以便您不必处理自定义标记,只需在connectionDidFinishLoading中检查网址本身即可。 – bizsytes

使用GCD和sendSynchronousRequest:请求,它们将在后台运行。

实施例:

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 
});