Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?

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.

like image 291
ozzotto Avatar asked Nov 22 '13 11:11

ozzotto


2 Answers

Objective-C

[manager.operationQueue cancelAllOperations];

Swift

manager.operationQueue.cancelAllOperations()
like image 145
Gary Lyn Avatar answered Jan 01 '23 05:01

Gary Lyn


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;
}
like image 32
David Avatar answered Jan 01 '23 03:01

David