setting limit on concurrent tasks in AFNetworking 2 running AFHTTPSessionManager(在运行 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.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]方法中启动该任务 - 创建一个 Grand Central 串行队列并在此队列中创建和启动任务
如果你的请求都发往同一个主机,直接访问基础 URL 加载系统中的
HTTPMaximumConnectionsPerHost选项,如下所示:
- Use
AFHTTPRequestOperationManagerinstead (this is what I'm doing) - Build an
NSOperationsubclass that has a task as a property, and start the task in the[operation start]method of your subclass - Create a Grand Central serial queue and create and start tasks in this queue
If your requests are all to the same host, directly access the
HTTPMaximumConnectionsPerHostoption 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 中设置并发任务限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在运行 AFHTTPSessionManager 的 AFNetworking 2 中设置并发任务限制
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
