因此,我正在使用 AFNetworking 2.0 重写 iOS 7 的应用程序,但遇到了一次发送一批请求并跟踪其进度的问题。在旧的 AFNetworking 中有enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:
方法上AFHTTPClient
,这显然已经被重构了,我对如何将多个请求排队有点困惑。
我创建了一个子类AFHTTPSessionManager
我正在使用POST:...
and GET:...
与服务器通信的方法。但我在代码和/或文档中找不到任何东西可以像旧的那样一次将多个请求排入队列AFHTTPClient
.
我唯一能找到的是无证的batchOfRequestOperations:progressBlock:completionBlock:
方法上AFURLConnectionOperation
,但这看起来像是 iOS 6 的做法。
显然我在新版本中遗漏了一些东西NSURLSession
我应该用它来批量请求或查看新的 AFNetworking 功能。希望有人可以帮助我走上正确的道路!
tl;dr:我怎样才能用我的设备发送一批请求AFHTTPSessionManager
子类?
感谢 Sendoa 提供的链接Mattt 解释的 GitHub 问题 https://github.com/AFNetworking/AFNetworking/issues/1504为什么这个功能不再起作用了。有一个明确的原因可以解释为什么新的系统不可能做到这一点NSURLSession
结构;任务不是操作,因此使用依赖项或批量操作的旧方法将不起作用。
我已经使用创建了这个解决方案dispatch_group
这使得可以使用批量请求NSURLSession
,这是(伪)代码:
// Create a dispatch group
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 10; i++) {
// Enter the group for each request we create
dispatch_group_enter(group);
// Fire the request
[self GET:@"endpoint.json"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
// Leave the group as soon as the request succeeded
dispatch_group_leave(group);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
// Leave the group as soon as the request failed
dispatch_group_leave(group);
}];
}
// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// Do whatever you need to do when all requests are finished
});
我想写一些东西,让这件事更容易做到,并与 Matt 讨论这是否可以合并到 AFNetworking 中(如果实现得很好)。在我看来,对图书馆本身做这样的事情会很棒。但我必须检查一下我什么时候有空闲时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)