16

Is there a way to cancel all network request (the request started by another method) before I do a network request with AFNetworking I tried like below but not work:

- (void)sendRequest:(NSUInteger)page{


NSURL *aUrl = [NSURL URLWithString:@"http://www.abc.com/"];
AFHTTPClient *httpClientToCancel = [[AFHTTPClient alloc] initWithBaseURL:aUrl];
[httpClientToCancel cancelAllHTTPOperationsWithMethod:@"POST" path:@"product/like"];
[httpClientToCancel release];

... start a new request here .....

But not work. I just want to cancel all request (at least the request I wrote above) before I start a new request.

Thank you!

Jason Zhao
  • 1,278
  • 4
  • 19
  • 36

3 Answers3

37

[[httpClient operationQueue] cancelAllOperations];

mattt
  • 19,544
  • 7
  • 73
  • 84
  • 1
    does this work? I am not sure if my requests are canceling by creating a new instance of httpClient and calling cancelAllOperations. – Alan Jul 23 '13 at 17:40
  • 2
    This is not working for me... the data keeps syncing to the server even after calling this function. Please help!! – Anshul Sep 23 '13 at 10:02
  • 3
    I think, it's important to mention here, that the failure block of all the canceled operations is still called, and you have to check the error code for -999 which stands for 'operation canceled'. So canceling operations doesn't prevent running the failure block! – matths Dec 05 '13 at 07:42
11

Don't Create new AFHTTPClient instance.

try "[self cancelAllHTTPOperationsWithMethod:@"POST" path:@"product/like"];

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Kaniel
  • 119
  • 1
  • 2
1

Both the other two answers are right. Don't Create new AFHTTPRequestOperationManager instance

        @interface OperateCustomerView () <WYPopoverControllerDelegate>{

        AFHTTPRequestOperationManager *manager;// = [AFHTTPRequestOperationManager manager];
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    manager = [AFHTTPRequestOperationManager manager];
Gank
  • 4,507
  • 4
  • 49
  • 45