AFNetworking 2:如何取消AFHTTPRequestOperationManager请求? [英] AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?

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

问题描述

我将网络功能从 AFNetworking 迁移到了 AFNetworking v2 ,而不是将 AFHttpClient 我也在使用 AFHTTPRequestOperationManager 来支持iOS6。

I migrated my networking functionality from AFNetworking to AFNetworking v2 and instead of AFHttpClient I am using AFHTTPRequestOperationManager to support iOS6 as well.

我的问题是 AFHttpClient 具有使用

- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path;

方法,在 AFHTTPRequestOperationManager 中没有这种明显的方法。

method, in the AFHTTPRequestOperationManager there is no such obvious method.

我到目前为止所做的是将 AFHTTPRequestOperationManager 子类化并声明一个iVar

What I've done up to now is subclassing AFHTTPRequestOperationManager and declaring an iVar

AFHTTPRequestOperation *_currentRequest;

当我发出请求时,代码类似于

When I make a request the code is something like

- (void)GetSomething:(NSInteger)ID success:(void (^)(MyResponse *))success failure:(void (^)(NSError *))failure
{
    _currentRequest = [self GET:@"api/something" parameters:@{@"ID": [NSNumber numberWithInteger:ID]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyResponse *response = [MyResponse responseFromDictionary:responseObject];
        success(response);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);

    }];
}

我有一个

- (void)cancelCurrentRequest;

所能做的所有方法

- (void)cancelCurrentRequest
{
    if(_currentRequest) {
        [_currentRequest cancel]; _currentRequest = nil;
    }
}

现在,我认为这不是好习惯并在调用该方法时得到(NSURLErrorDomain错误-999。),这就是为什么我需要一些正确完成此操作的建议的原因。

Now, I don't think this is good practice and when the method is called I get (NSURLErrorDomain error -999.) which is why I need some advice on getting this done correctly.

谢谢。

推荐答案

[manager.operationQueue cancelAllOperations];

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

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