在运行AFHTTPSessionManager的AFNetworking 2中设置对并发任务的限制 [英] setting limit on concurrent tasks in AFNetworking 2 running AFHTTPSessionManager

查看:1010
本文介绍了在运行AFHTTPSessionManager的AFNetworking 2中设置对并发任务的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道在旧的AFNetworking中这可以使用AFHTTPClient,
而且我知道如果我使用AFHTTPRequestOperationManager我可以设置队列的限制,但我不能让AFHTTPSessionManager只运行x请求没有自己使用成功块(我不想要)实现它的时间。

so I know that in the old AFNetworking this was possible using the AFHTTPClient, and I know that if I use AFHTTPRequestOperationManager I can set the queue's limit, but I can't make AFHTTPSessionManager to run only x requests at a time without implementing it by myself using the success block (which I don't want to).

以下代码没有限制我的连接:

The following code did NOT limit my connections:


AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.operationQueue.maxConcurrentOperationCount = 1;

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.operationQueue.maxConcurrentOperationCount = 1;

与有趣的讨论一致在哪里,我向我的服务器发出了很多请求,直到我得到超时才扼杀它,所以我真的需要限制我的并发连接。

In line with an interesting discussion here, I have a lot of requests to my server and I choke it until I get timeouts, so I really need to limit my concurrent connections.

我缺少什么?

推荐答案

AFHTTPSessionManager 使用任务而不是操作(具体来说, NSURLSessionDataTask ),这就是你无法设置操作队列的原因。

AFHTTPSessionManager uses tasks instead of operations (NSURLSessionDataTask, specifically), which is why you can't set an operation queue.

正如您在此课程的实施中所见,任务立即启动( [任务恢复] ),而不是添加到任何类型的队列。

As you can see in the implementation of this class, tasks are immediately started ([task resume]) and not added to any sort of queue.

因此,不幸的是,没有内置的AFNetworking方法来设置限制使用 AFHTTPSessionManager 的并发任务数。

Consequently, and unfortunately, there is no built-into-AFNetworking way to set a limit to the number of concurrent tasks using AFHTTPSessionManager.

可能的替代方案:


  1. 使用 AFHTTPRequestOperationManager 代替(这就是我正在做的事)

  2. 建立一个 NSOperation 将任务作为属性的子类,并在子类的 [operation start] 方法中启动任务

  3. 创建一个Grand Central串行队列并在此队列中创建和启动任务

  4. 如果您的请求都是同一主机,则直接访问基础URL加载系统中的 HTTPMaximumConnectionsPerHost 选项,如下所示:

  1. Use AFHTTPRequestOperationManager instead (this is what I'm doing)
  2. Build an NSOperation subclass that has a task as a property, and start the task in the [operation start] method of your subclass
  3. Create a Grand Central serial queue and create and start tasks in this queue
  4. If your requests are all to the same host, directly access the HTTPMaximumConnectionsPerHost option in the foundation URL loading system, like so:

[NSURLSessionConfiguration defaultSessionConfiguration].HTTPMaximumConnectionsPerHost = 4;

这种方法有一些注意事项,在 Apple文档

This approach has a number of caveats, which are discussed in the Apple documentation.

如果您最终做#2,请将其作为拉动请求提交给AFNetworking - 这将是一个受欢迎的补充。

If you wind up doing #2, please submit it as a pull request to AFNetworking - it would be a welcome addition.

这篇关于在运行AFHTTPSessionManager的AFNetworking 2中设置对并发任务的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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