Ios 多个ASITPPrequest

Ios 多个ASITPPrequest,ios,asihttprequest,Ios,Asihttprequest,我意识到这两个URL不会同时返回数据。我想知道如何才能确保URL同时返回。我怎样才能将此代码更改为GCD。建议这样做吗?我已经在代码中创建了调度队列,但它没有绘制任何内容,无法工作。如果没有GCD,它可以工作,但不会同时返回。任何帮助都将不胜感激 -(void)httpRequest{ _weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1]; [request1 setCompletionBlock:^{

我意识到这两个URL不会同时返回数据。我想知道如何才能确保URL同时返回。我怎样才能将此代码更改为GCD。建议这样做吗?我已经在代码中创建了调度队列,但它没有绘制任何内容,无法工作。如果没有GCD,它可以工作,但不会同时返回。任何帮助都将不胜感激

-(void)httpRequest{

_weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
[request1 setCompletionBlock:^{
    NSString *responseString1 = [request1 responseString];
//dispatch_async(backgroundProcess1,^(void){
    [self plotOverlay1:responseString1];
 //});
}];
[request1 setFailedBlock:^{
    NSError *error=[request1 error];
    NSLog(@"Error: %@", error.localizedDescription);
}]; 

[request1 startAsynchronous];


_weak ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
[request2 setCompletionBlock:^{
    NSString *responseString2 = [request2 responseString];
 //dispatch_async(backgroundProcess2,^(void){
    [self plotOverlay2:responseString2];
  //});
}];
[request2 setFailedBlock:^{
    NSError *error=[request2 error];
    NSLog(@"Error: %@", error.localizedDescription);
}]; 

[request2 startAsynchronous];


}

您无法保证两个独立的异步请求将同时返回

一种解决方案是链接请求——换句话说,在第一个请求的完成块中启动第二个请求——然后在第二个请求的完成块中对两个请求进行处理

允许两个请求并行运行的另一种方法是在类上创建属性来保存请求的响应。在请求完成处理程序中,将相应的属性设置为响应字符串的值,然后调用新方法。该方法将检查两个请求的responseString属性是否都有值,如果有,则执行处理,如果没有,则不执行任何操作。这样,每当一个请求完成时(哪一个先完成并不重要),都不会发生任何事情,但一旦另一个请求也完成了,您的处理就会完成(对于两个请求)

-(void)httpRequest
{
    _weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
    [request1 setCompletionBlock:^{
        self.responseString1 = [request1 responseString];
        [self checkIfBothRequestsComplete];
    }];
    [request1 startAsynchronous];

    _weak ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
    [request2 setCompletionBlock:^{
        self.responseString2 = [request2 responseString];
        [self checkIfBothRequestsComplete];
    }];
    [request2 startAsynchronous];
}

- (void)checkIfBothRequestsComplete
{
    if (self.responseString1 && self.responseString2) {
        [self plotOverlay1:self.responseString1];
        [self plotOverlay2:self.responseString2];
    }

}

非常感谢乔恩·克罗尔。我真的很感激。我应该认为这个解决方案简单明了。现在我肯定这两个url同时返回数据。我创建的方法(httpRequest)调用url的问题是由计时器调用的。然而,计时器调用(httpRequest)每4秒钟一次,但是如果url响应执行繁重的任务,则它有时不匹配。例如,timer=[NSTimer scheculedWith TimerInterval(4.0)target:(self)selector:@selector(httpRequest)userinfo:nil repeats:YES]。解决该问题的最佳方案是什么。提前谢谢