AFNetworking + cancelAllRequests

天大地大妈咪最大 提交于 2019-12-21 03:32:00

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/9583743/afnetworking-cancelallrequests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!