【问题标题】:AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?AFNetworking 2:如何取消 AFHTTPRequestOperationManager 请求?
【发布时间】:2013-12-07 06:36:21
【问题描述】:

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

我的问题是,在 AFHttpClient 中,有一个功能可以使用

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

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

到目前为止,我所做的是继承 AFHTTPRequestOperationManager 并声明一个 iVar

AFHTTPRequestOperation *_currentRequest;

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

- (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 error -999.),这就是为什么我需要一些建议来正确完成此操作。

提前谢谢你。

【问题讨论】:

    标签: afnetworking-2


    【解决方案1】:

    Objective-C

    [manager.operationQueue cancelAllOperations];
    

    斯威夫特

    manager.operationQueue.cancelAllOperations()
    

    【讨论】:

    • 谢谢你的答案。我注意到了,但是如果您想取消特定的 GET/POST 请求怎么办?
    • 如果你查看 cancelAllHTTPOperationsWithMethod:path 的实现,你会发现如何去做。它只是迭代 operationQueue 中的所有操作以找到与方法和路径匹配的操作并取消它们。他们在 v2 中删除它们的原因我猜他们认为没有必要取消特定操作?
    • 这就是我最终所做的。 cancelAllHTTPOperationsWithMethod:path 在我的 AFHTTPRequestOperationManager 子类中的实现。谢谢队友。
    【解决方案2】:

    您不必继承 AFHTTPRequestOperationManager ,因为当您发送请求时,AFHTTPRequestOperation

    - (AFHTTPRequestOperation *)GET:(NSString *)URLString
                         parameters:(id)parameters
                            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
    

    只需将其保存在某处或设为静态,然后在需要取消请求时执行cancel

    例子:

    - (void)sendRequestToDoSomething
    {
       static AFHTTPRequestOperation *operation;
       if(operation) //cancel operation if it is running
           [operation cancel];
       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      //configure manager here....
    
    operation = [manager GET:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
       //do something here with the response
       operation = nil;
    } failure:^(AFHTTPRequestOperation *op, NSError *error) {
    {
       //handle error
       operation = nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多