Multiple ASIHTTPRequests

2019-06-09 19:54发布

问题:

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


}

回答1:

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.

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

}