使用NSOperation AFNetworking 3.0 AFHTTPSessionManager [英] AFNetworking 3.0 AFHTTPSessionManager using NSOperation

查看:268
本文介绍了使用NSOperation AFNetworking 3.0 AFHTTPSessionManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在被困住了一段时间,我需要帮助。所以在AFNetworking 2.0中我们有 AFHTTPRequestOperation 所以我可以很容易地使用 NSOperationQueue 并且有一些依赖。所以我们现在只有 AFHTTPSessionManager NSURLSession ,它不是子类 NSOperation 。我有类 APIClient 那个子类 AFHTTPSessionManager 。我使用该类作为单身作为 sharedClient 。我已经覆盖了GET和POST,所以例如GET看起来:

I'm stuck now some time and I need help. So in AFNetworking 2.0 we have AFHTTPRequestOperation so I could easily use NSOperationQueue and have some dependencies. So what we have now is only AFHTTPSessionManagerand NSURLSession that does not subclass NSOperation. I have class APIClient that subclasses AFHTTPSessionManager. I am using that class as singleton as sharedClient. I have overriden GET and POST so for example GET looks this:

- (NSURLSessionDataTask *)GET:(NSString *)URLString
               parameters:(NSDictionary *)parameters
                  success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                  failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
NSURLSessionDataTask *task = [super GET:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
    success(task, responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    failure(task, [Response createErrorWithAFNetworkingError:error]);
}];

return task;
}

您是否知道如何以这种方式实施(如果可能的话)把它换成 NSOperation ?所以我想做什么 - 我希望能够并行运行两个网络呼叫,然后再进行另一个方法调用,这取决于前两个呼叫的第二次网络呼叫。你知道什么是最好的方法吗?

Do you have any idea how to implement in that manner (if it's possible) to wrap that as NSOperation? So what I want to do - I want to be able to run in parallel two network calls, and after that have another method call that depends on second network call of first two calls. Do you have any idea what would be best approach?

推荐答案

我写了一小组快速的课程( https://github.com/robertmryan/AFHTTPSessionOperation/ 包装 AFHTTPSessionManager 请求在异步 NSOperation 子类中。然后,您可以使用它来享受 maxConcurrentOperation 约束或操作依赖性。

I've written a quick little set of classes (https://github.com/robertmryan/AFHTTPSessionOperation/) that wrap AFHTTPSessionManager requests in asynchronous NSOperation subclass. You can then use that to enjoy maxConcurrentOperation constraints, or operation dependencies.

例如,这是一个示例,我们发出两个并发请求,并且完成操作取决于这两个请求的完成情况:

For example, here's an example where we issue two concurrent requests and have a completion operation dependent upon completion of both of those requests:

//  ViewController.m

#import "ViewController.h"
#import "AFNetworking.h"
#import "AFHTTPSessionOperation.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *urlString1 = @"...";
    NSString *urlString2 = @"...";

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.name = @"AFHTTPSessionManager queue";

    NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"All done");
    }];

    NSOperation *op1 = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"GET" URLString:urlString1 parameters:nil uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"finished 1");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"failed 1 - error = %@", error.localizedDescription);
    }];
    [completionOperation addDependency:op1];

    NSOperation *op2 = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"GET" URLString:urlString2 parameters:nil uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"finished 2");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"failed 2 - error = %@", error.localizedDescription);
    }];
    [completionOperation addDependency:op2];

    [queue addOperations:@[op1, op2] waitUntilFinished:false];
    [[NSOperationQueue mainQueue] addOperation:completionOperation];  // do this on whatever queue you want, but often you're updating UI or model objects, in which case you'd use the main queue
}

@end

值得注意的是,由于您只处理两个请求,您还可以使用调度组来完成同样的事情:

It's worth noting that since you're only dealing with two requests, you could also use dispatch groups to accomplish the same thing:

//  ViewController.m

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *urlString1 = @"...";
    NSString *urlString2 = @"...";

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_enter(group);
    [manager GET:urlString1 parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"finished 1");
        dispatch_group_leave(group);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed 1 - error = %@", error.localizedDescription);
        dispatch_group_leave(group);
    }];

    dispatch_group_enter(group);
    [manager GET:urlString2 parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"finished 2");
        dispatch_group_leave(group);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed 2 - error = %@", error.localizedDescription);
        dispatch_group_leave(group);
    }];

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"All done");
    });
}

@end

对于调度组,你只需要需要注意的是, success 失败块中的每个路径都会调用 dispatch_group_leave

With dispatch groups, you just need to be careful that every path within both the success and failure blocks call dispatch_group_leave.

这篇关于使用NSOperation AFNetworking 3.0 AFHTTPSessionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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