15

I really have a problem when I want to stop all current requests in a sync engine built with AFNetworking.

I have 5 different URL to query. Each query is launch if the previous was correctly executed.

This works very well.

I want to stop the sync process at anytime. So my code to do that is:

- (void)cancelAllRequests
{
  NSLog(@"CancelAllRequests");

  [[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull"];
  [[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull_items"];
  [[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull_image"];
  [[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"POST" path:@"ws/webapp/services/push_item"];  
  [[[HTTPClient sharedClient] operationQueue] cancelAllOperations];
}

But this code seems to do nothing. When I want to cancel, I saw all the batch operations working in my logs after the method is called.

What did I miss ? If I cancel the requests, this don't stop all active operations build with this requests ?

John Topley
  • 113,588
  • 46
  • 195
  • 237
alex.bour
  • 2,842
  • 9
  • 40
  • 66
  • What is HTTPClient? Does it belong to AFNetworking ? – Jason Zhao Jun 15 '12 at 09:01
  • 2
    I think what he meant was: AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; [[httpClient operationQueue] cancelAllOperations]; – Flaviu Jul 14 '12 at 12:26

1 Answers1

26

You should only need to do [[[HTTPClient sharedClient] operationQueue] cancelAllOperations]. Operations when they're cancelled attempt to finish execution as possible, but there's no guarantee about exactly how that happens. In the case of batch operations, it may already be finishing by the time it gets cancelled because all of its dependency request operations have finished (by being cancelled).

mattt
  • 19,544
  • 7
  • 73
  • 84
  • mattt, I have operation 1 => if ok operation 2 => if ok => operation 3 => if ok => operation queue with a lot of same kinds of operations. That's why I can't know exactly when the user stops the operations... so if it is operation 1 or 2, I have no queue, that's why I put [[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull"]; before deleting the queue. – alex.bour Mar 07 '12 at 07:24
  • Tanks program with unrecognized selector sent to instance. – Oh Danny Boy Oct 20 '12 at 17:28