NSURLConnection sendAsynchronousRequest:queue:completionHandler:连续发出多个请求? [英] NSURLConnection sendAsynchronousRequest:queue:completionHandler: making multiple requests in a row?

查看:100
本文介绍了NSURLConnection sendAsynchronousRequest:queue:completionHandler:连续发出多个请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何在使用这种伟大的异步方法的同时做到这一点?

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

推荐答案

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

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。如果你使用其中任何一个,你应该添加一些错误检查。

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

这篇关于NSURLConnection sendAsynchronousRequest:queue:completionHandler:连续发出多个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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