所以我知道在旧的 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.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.
正如你在这个类的实现中看到的,任务会立即启动([task resume]
)并且不会添加到任何类型的队列中.
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
.
可能的替代方案:
AFHTTPRequestOperationManager
代替(这就是我正在做的)NSOperation
子类,并在您的子类的[operation start]
方法中启动该任务如果你的请求都发往同一个主机,直接访问基础 URL 加载系统中的 HTTPMaximumConnectionsPerHost
选项,如下所示:
AFHTTPRequestOperationManager
instead (this is what I'm doing)NSOperation
subclass that has a task as a property, and start the task in the [operation start]
method of your subclassIf 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 中设置并发任务限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!