ios NSURLConnection sendAsynchronousRequest:queue:completionHandler:连续发出多个请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9409211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:58:39  来源:igfitidea点击:

NSURLConnection sendAsynchronousRequest:queue:completionHandler: making multiple requests in a row?

objective-ciosios5nsurlconnection

提问by Nic Hubbard

I have been using NSURLConnection'ssendAsynchronousRequest:queue:completionHandler:method which is great. But, I now need to make multiple requests in a row.

我一直在使用NSURLConnection'ssendAsynchronousRequest:queue:completionHandler:很棒的方法。但是,我现在需要连续发出多个请求。

How can I do this while still using this great asychronous method?

如何在仍然使用这种出色的异步方法的同时做到这一点?

回答by yuji

There's lots of ways you can do this depending on the behavior you want.

根据您想要的行为,有很多方法可以做到这一点。

You can send a bunch of asynchronous requests at once, track the number of requests that have been completed, and do something once they're all done:

您可以一次发送一堆异步请求,跟踪已完成的请求数量,并在它们全部完成后做一些事情:

NSInteger outstandingRequests = [requestsArray count];
for (NSURLRequest *request in requestsArray) {
    [NSURLConnection sendAsynchronousRequest:request 
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [self doSomethingWithData:data];
        outstandingRequests--;
        if (outstandingRequests == 0) {
            [self doSomethingElse];
        }
    }];
}

You could chain the blocks together:

您可以将这些块链接在一起:

NSMutableArray *dataArray = [NSMutableArray array];    
__block (^handler)(NSURLResponse *response, NSData *data, NSError *error);

NSInteger currentRequestIndex = 0;
handler = ^{
    [dataArray addObject:data];
    currentRequestIndex++;
    if (currentRequestIndex < [requestsArray count]) {
        [NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:currentRequestIndex] 
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:handler];
    } else {
        [self doSomethingElse];
    }
};
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:0] 
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:handler];

Or you could do all the requests synchronously in an ansynchronous block:

或者您可以在异步块中同步执行所有请求:

dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("Lots of requests", NULL);
    dispatch_async(downloadQueue, ^{
        for (NSRURLRequest *request in requestsArray) {
            [dataArray addObject:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]];
        }
        dispatch_async(callerQueue, ^{
            [self doSomethingWithDataArray:dataArray];
        });
    });
});

P.S. If you use any of these you should add some error checking.

PS 如果您使用其中任何一个,您应该添加一些错误检查。