Mac*_*n13 49 ios afnetworking afnetworking-2
因此,我正在使用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子类发送一批请求?
Mac*_*n13 85
感谢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
});
Run Code Online (Sandbox Code Playgroud)
我想写一些能让你更容易做的事情,并与Matt讨论,如果这是可以合并到AFNetworking的东西(当很好地实现时).在我看来,用图书馆本身做这样的事情会很棒.但我必须检查一下我有空闲时间.
| 归档时间: |
|
| 查看次数: |
17943 次 |
| 最近记录: |