iOS以任何方式取消所有网络请求 [英] iOS any way to cancel all network requests

查看:498
本文介绍了iOS以任何方式取消所有网络请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行应用程序更新模块。我想做的是:

I'm doing the app update module. What I want to do is:

如果检测到较新的版本,请提示用户更新并同时取消所有网络请求(如果存在)。该请求可以由AFNETWORK创建,也可以像[NSURLConnection sendSynchronousRequest:returningResponse:error:]这样创建,所有类型的网络请求都只需将其全部取消即可。我可以吗?

if there is a newer version detect, prompt user update AND at the same time, cancel all network requests if there is. The request may created by AFNETWORK, may created like [NSURLConnection sendSynchronousRequest:returningResponse:error:], all kind of network request, just cancel them all. Can I?

推荐答案

使用 +(NSData *)sendSynchronousRequest:(NSURLRequest *)requestingingResponse创建的连接:(NSURLResponse **)响应错误:(NSError **)错误无法取消。如果从后台线程调用它,则可以杀死该线程本身。

Connections created using + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error cannot be canceled. If you called it from a background thread, you may be able to kill the thread itself.

更好的方法是使用NSOperationQueue并从添加到队列中的NSOperation对象启动网络连接。 (可选)您可以创建NSOperation的子类,并使每个实例以其 cancel 方法为目标来观察自定义事件通知。使用这种方法,您只需要发布自定义通知即可触发所有操作实例及其关联的网络连接的取消。

A better approach would be to use NSOperationQueue and initiate network connections from NSOperation objects added to the queue. Optionally, you can create a subclass of NSOperation and make each instance observe a custom event notification with its cancel method as the target. With this approach, you will just need to post the custom notification to trigger a cancel to all instances of your operations and their associated network connections.

这篇关于iOS以任何方式取消所有网络请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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