AFNetworking + cancelAllRequests [英] AFNetworking + cancelAllRequests

查看:82
本文介绍了AFNetworking + cancelAllRequests的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想停止使用AFNetworking构建的同步引擎中的所有当前请求时,我确实遇到了问题。

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

我有5个不同的URL可以查询。如果前一个查询已正确执行,则将启动每个查询。

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

这很好。

我想停止随时进行同步处理。
所以我要做的代码是:

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 ?

推荐答案

您只需要这样做 [[[HTTPClient sharedClient] operationQueue] cancelAllOperations] 。取消操作后,它们会尝试完成执行,但不能保证确切的执行过程。对于批处理操作,由于它的所有依赖项请求操作都已经完成(通过取消),它可能在取消之前就已经完成了。

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

这篇关于AFNetworking + cancelAllRequests的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆