37

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

My issue is that while in AFHttpClient there was the functionality to cancel a pending request using the

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

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

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);

    }];
}

and I have a

- (void)cancelCurrentRequest;

methods which all that does is

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

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.

Thank you in advance.

ozzotto
  • 1,146
  • 3
  • 13
  • 30

2 Answers2

80

Objective-C

[manager.operationQueue cancelAllOperations];

Swift

manager.operationQueue.cancelAllOperations()
Gary Lyn
  • 1,088
  • 9
  • 11
  • 3
    Thx for the answer. I 've noticed that but what if you want to cancel a specific GET/POST request? – ozzotto Nov 22 '13 at 11:53
  • 3
    If you look into the implementation of cancelAllHTTPOperationsWithMethod:path, you'll find how to do it. It just iterate all the operations in operationQueue to find ones that match the method and path and cancel them. The reason they remove them in v2 I guess it that they think it's unnecessary to cancel a specific operation? – Gary Lyn Nov 22 '13 at 11:58
  • That's what I ended up doing. An implementation of cancelAllHTTPOperationsWithMethod:path in my AFHTTPRequestOperationManager subclass. Thx mate. – ozzotto Nov 22 '13 at 13:28
2

You don't have to subclass AFHTTPRequestOperationManager , because when you send request, AFHTTPRequestOperation returns from the

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

simply save it somewhere or make static and then perform cancel when the request need to be canceled.

Example:

- (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;
}
David
  • 1,061
  • 11
  • 18