如何使用AFNetworking 2批量请求? [英] How to batch request with AFNetworking 2?

查看:175
本文介绍了如何使用AFNetworking 2批量请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用AFNetworking 2.0重写iOS 7的应用程序,我遇到了一次发送一批请求并跟踪其进度的问题。在旧的AFNetworking中有 enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:方法 AFHTTPClient ,这显然是重构的,我我对如何排队多个请求感到有点困惑。

So I'm rewriting an app for iOS 7 with AFNetworking 2.0 and I'm running into the issue of sending a batch of requests at once and tracking their progress. In the old AFNetworking there was the enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock: method on AFHTTPClient, this is clearly refactored out and I'm a bit confused on how to enqueue multiple requests.

我创建了一个 AFHTTPSessionManager 的子类,我m使用 POST:... GET:... 与服务器通信的方法。但我无法在代码和/或文档中找到任何内容,可以像使用旧的 AFHTTPClient 一样将多个请求排入队列。

I have created a subclass of AFHTTPSessionManager and I'm using the POST:... and GET:... methods to communicate with the server. But I can't find anything in the code and/or docs to enqueue multiple requests at once like with the old AFHTTPClient.

我唯一能找到的是 batchOfRequestOperations:progressBlock:completionBlock: AFURLConnectionOperation 上的方法,但是看起来像iOS 6这样做的方式。

The only thing I can find is the undocumented batchOfRequestOperations:progressBlock:completionBlock: method on AFURLConnectionOperation, but that looks like the iOS 6 way of doing this.

显然我在新的 NSURLSession 概念中遗漏了一些我应该用来批量请求或查看新的AFNetworking功能。希望有人能在这里帮助我走上正轨!

Clearly I'm missing something in the new NSURLSession concept that I should use to batch requests or looking over a new AFNetworking feature. Hope someone can help me on the right track here!

tl;博士:如何用我的<$ c发送一批请求$ c> AFHTTPSessionManager 子类?

推荐答案

感谢Sendoa获取 Matit解释的GitHub问题为什么这个功能不再起作用了。有一个明确的原因,为什么新的 NSURLSession 结构无法做到这一点;任务只是不是操作,因此使用依赖关系或批量操作的旧方法将无法工作。

Thanks Sendoa for the link to the GitHub issue where Mattt explains why this functionality is not working anymore. There is a clear reason why this isn't possible with the new NSURLSession structure; Tasks just aren't operations, so the old way of using dependencies or batches of operations won't work.

我使用<$ c $创建了此解决方案c> dispatch_group 可以使用 NSURLSession 批量处理请求,这里是(伪)代码:

I've created this solution using a dispatch_group that makes it possible to batch requests using NSURLSession, here is the (pseudo-)code:

// 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。在我看来,用图书馆本身做这样的事情会很棒。但是我必须检查一下我有空闲时间。

I want to look write something that makes this easier to do and discuss with Matt if this is something (when implemented nicely) that could be merged into AFNetworking. In my opinion it would be great to do something like this with the library itself. But I have to check when I have some spare time for that.

这篇关于如何使用AFNetworking 2批量请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆