一对一发送请求 [英] Send a bunch of requests one-by-one

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

问题描述

我需要一次将100个网络请求发送到我的服务器,并在第100个完成时得到通知.

I need to send 100 network requests to my server one-by-one and get notified when the 100th is done.

我正在使用AFNetworking,正在考虑解决此问题的方法.有人可以推荐我一些东西吗?

I'm using AFNetworking and was thinking about a solution of this problem. Can anyone recommend me something?

推荐答案

一些想法:

  1. 如果真的只是要连续(即一个接一个)地运行每个请求,则可以执行以下操作:

  1. If really just going to run each request serially (i.e. one after another), you could do:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;

NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"All operations done");
}];

for (NSInteger i = 0; i < operationCount; i++) {
    AFHTTPRequestOperation *operation = ... // create your operation here

    [completionOperation addDependency:operation];

    [queue addOperation:operation];
}

[queue addOperation:completionOperation];

请注意,使用这样的操作队列具有以下优点:您可以在需要时轻松取消该队列中的所有操作.

Note, using operation queue like this offers the advantage that you can easily cancel all the operations in that queue should you ever need to.

如果执行这些命令的顺序很关键,则可能需要在操作之间建立显式依赖关系,例如:

If the order that these are performed is critical, you might want to establish explicit dependencies between the operations, e.g.:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;

NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"All operations done");
}];

NSOperation *priorOperation = nil;

for (NSInteger i = 0; i < operationCount; i++) {
    AFHTTPRequestOperation *operation = ... // create your operation here

    [completionOperation addDependency:operation];

    if (priorOperation) [operation addDependency:priorOperation];

    [queue addOperation:operation];

    priorOperation = operation;
}

[queue addOperation:completionOperation];

  • 对我来说,问题是您是否绝对只想一次运行一个.您为此付出了巨大的性能损失.通常,您会使用第一个代码示例(其中唯一的显式依赖项是对完成操作的依赖),并将maxConcurrentOperationCount设置为4之类的东西,从而享受并发性以及随之而来的显着性能提升(同时限制了并发程度达到一定的合理数量,不会耗尽您的所有工作线程,也有可能导致请求超时等).

  • The question for me is whether you absolutely only want to run one at a time. You pay a significant performance penalty for that. Generally you'd use that first code sample (where the only explicit dependencies are to the completion operation) and set maxConcurrentOperationCount to something like 4, enjoying concurrency and its consequent significant performance gain (while at the same time, constraining the degree of concurrency to some reasonable number that won't use up all of your worker threads, risk having requests time out, etc.).

    您还没有说这100个操作是什么,但是如果下载量很大,则可能需要考虑延迟加载"模式,根据需要异步加载数据,而不是全部加载一次.

    You haven't said what these 100 operations are, but if it's a bunch of downloads, you might want to consider a "lazy loading" pattern, loading the data asynchronously as you need it, rather than all at once.

    例如,如果下载图像,则可以使用AFNetworking UIImageView类别来实现.

    If downloading images, for example, you might achieve this using the AFNetworking UIImageView category.

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

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