gpt4 book ai didi

ios - AFNetworking 3.0 AFHTTPSessionManager 使用 NSOperation

转载 作者:技术小花猫 更新时间:2023-10-29 10:19:51 27 4
gpt4 key购买 nike

我现在卡住了一段时间,需要帮助。所以在 AFNetworking 2.0 中我们有 AFHTTPRequestOperation 所以我可以很容易地使用 NSOperationQueue 并且有一些依赖。所以我们现在只有 AFHTTPSessionManagerNSURLSession 没有继承 NSOperation。我有 APIClient 类,它是 AFHTTPSessionManager 的子类。我将该类用作 sharedClient 的单例。我已经覆盖了 GET 和 POST 所以例如 GET 看起来是这样的:

- (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?所以我想做的 - 我希望能够并行运行两个网络调用,然后有另一个方法调用取决于前两个调用的第二个网络调用。您知道什么是最好的方法吗?

最佳答案

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

例如,这是一个示例,其中我们发出两个并发请求,并有一个依赖于这两个请求完成的完成操作:

//  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

值得注意的是,因为你只处理两个请求,你也可以使用调度组来完成同样的事情:

//  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

对于调度组,您只需要注意 successfailure block 中的每条路径都调用 dispatch_group_leave

关于ios - AFNetworking 3.0 AFHTTPSessionManager 使用 NSOperation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34753816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com