因此,我正在使用AFNetworking 2.0重写iOS 7的应用程序,并且遇到了一次发送一批请求并跟踪其进度的问题。在旧的AFNetworking中,存在enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:
方法AFHTTPClient
,显然已将其重构,而我对如何使多个请求入队有些困惑。
我创建了的子类,AFHTTPSessionManager
并使用POST:...
和GET:...
方法与服务器进行通信。但是我无法在代码和/或文档中找到任何内容来像旧版本一样一次将多个请求放入队列AFHTTPClient
。
我唯一能找到的是上的未记录batchOfRequestOperations:progressBlock:completionBlock:
方法AFURLConnectionOperation
,但这看起来像是iOS 6的方法。
显然,我没有使用新NSURLSession
概念来批处理请求或查看新的AFNetworking功能。希望有人能在正确的道路上帮助我!
tl; dr:如何使用AFHTTPSessionManager
子类发送一批请求?
感谢Sendoa提供到GitHub问题的链接,其中Mattt解释了为什么此功能不再起作用。很明显的原因为什么新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中(如果实现得当)。我认为最好对库本身做这样的事情。但是我必须检查我什么时候有空余时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句