多个ASIHTTPRequests [英] Multiple ASIHTTPRequests

查看:51
本文介绍了多个ASIHTTPRequests的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经意识到这两个url不会同时返回数据.我想知道如何确保网址同时返回.我怎么能将此代码更改为GCD.推荐吗?我已经在代码中创建了调度队列,但是它没有绘制任何内容,无法正常工作.如果没有GCD,它将起作用,但不会同时返回.任何帮助将不胜感激!

I have realized that these two urls do not returns data at the same time. I would like to know how I could able to make sure that urls return at the same time. How could I able to change this code to GCD. Is that recommended? I have created dispatch queue in the code but it does not plot anything, not working. without GCD it works but it does not return at the same time. Any help would be appreciated!

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


}

推荐答案

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

There's no way you can guarantee two independent asynchronous requests will return at the same time.

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

One solution would be to chain the requests -- in other words, start the second request in the completion block of the first request -- and then do processing for both requests in the completion block of the second request.

另一种允许两个请求并行运行的方法是在类上创建属性,以保存请求的响应.在请求完成处理程序中,您可以将适当的属性设置为响应字符串的值,然后调用新方法.该方法将检查两个请求的responseString属性是否都具有值,如果是,则进行处理,如果没有,则什么都不做.这样,只要一个请求完成就不会发生任何事情(无论哪个先完成都没有关系),但是一旦另一个请求也完成了,您的处理就将完成(对于两个请求).

Another way that would allow both requests to run in parallel would be to create properties on your class to hold the responses from the requests. In the request completion handlers you would set the appropriate property to the value of the response string, and then call a new method. That method would check to see if the responseString properties from both requests had a value, and if so, do your processing, and if not, do nothing. This way, nothing will happen whenever the one request finishes (and it won't matter which finishes first) but your processing will be done (for both requests) once the other request has also finished.

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

}

这篇关于多个ASIHTTPRequests的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆