我可以使用addOperationWithBlock取消添加到NSOperationQueue的块吗? [英] Can I cancel a Block added to an NSOperationQueue with addOperationWithBlock:?

查看:182
本文介绍了我可以使用addOperationWithBlock取消添加到NSOperationQueue的块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过许多文章,说块是未来!!!。我想知道它是否与在后台运行操作有关。

I've read many many articles which say "BLOCKS ARE THE FUTURE!!!". I'm wondering if it relates to running operations in the background.

例如,我有一个表格视图,其中包含来自网络的图像。现在我可以使用 + [NSOperationQueue addOperationWithBlock:] 来获取它们。当单元格变为可见时,操作将被发送到队列。但是有一种方法可以在单元格从屏幕滚动后取消它吗?或者是唯一的方法来进行子类化 NSOperation ?使用块是如此简单,所以在我尝试解决这之前我只是问这个问题。示例 NSOperation 子类......

For example, I have a table view which has images that will come from the web. Right now I can get them using +[NSOperationQueue addOperationWithBlock:]. An operation gets sent to the queue when the cell becomes visible. But is there a way to cancel it once the cell gets scrolled off the screen? Or is the only way to do it to subclass NSOperation? Using blocks is so easy, so I'm just asking this question before I try to tackle this example of NSOperation subclass...

推荐答案

问题似乎是你是否可以创建一个可取消的 NSBlockOperation 。正如此答案所示,引用 WWDC 2012会话#211,构建并发用户界面,你当然可以。

The question appears to be whether you can create a cancelable NSBlockOperation. As this answer suggests, quoting from WWDC 2012 session #211, Building Concurrent User Interfaces, you certainly can.

这种方法但是,它包含了一些限制。值得注意的是,您必须将取消逻辑放在块中。如果您的块正在运行某个循环,它可以重复检查 isCancelled 状态,这样可以正常工作。但是,如果你正处于一些网络请求的中间,那么在 NSBlockOperation 中执行将会很尴尬。

This approach consists of limitations, though. Notably, you have to put the cancellation logic in your block. This works fine if your block is running some loop in which it can repeatedly check the isCancelled status. But if you're in the middle of some network request, this is going to be awkward to perform in a NSBlockOperation.

使用其他答案(以及WWDC 2012视频)中列出的模式,您可以编写一个 NSBlockOperation ,它使用基于块的<$ c $的折磨组合c> NSURLSession 和一个轮询循环取消 NSURLSessionTask 如果操作被取消,这将完成你想要的,但这是一个可怕的解决方案(效率低下) ,繁琐,使用块中的取消逻辑来阻碍您的应用程序代码等。)

Using the pattern outlined in that other answer (and that WWDC 2012 video), you could write a NSBlockOperation which employed a tortured combination of block-based NSURLSession and a polling loop which cancels the NSURLSessionTask if the operation is canceled, which accomplishes what you intend, but it's a horrible solution (inefficient, cumbersome, encumbering your app code with cancellation logic in the block, etc.).

如果您想进行可取消的网络操作, NSOperation 子类将是一种更优雅的方式。第一次这样做,它看起来很麻烦,但是一旦你熟悉了这个模式,它就变成了第二个本质而且很容易实现。你会发现自己一次又一次地回到这种模式。请参阅并发编程指南的操作队列章节中的nofollow noreferrer>定义自定义操作对象部分,用于讨论如何进行可取消的并发操作,尤其是关于回应取消事件的讨论。

If you want to make a cancelable network operation, a NSOperation subclass is going to be a far more elegant way to do this. The first time you do this, it's going to seem cumbersome, but once you familiarize yourself with the pattern, it becomes second nature and trivial to implement. And you'll find yourself coming back to this pattern again and again. See the Defining a Custom Operation Object section of the Operation Queues chapter of the Concurrency Programming Guide for discussions about making cancelable, concurrent operations, notably the discussion about "Responding to Cancellation Events".

作为最后的观察,你将这个使用块和 NSOperation -subclass描述为或/或命题。但是,实际上,您实际上要将这两种技术结合起来,创建一个 NSOperation 子类,该子类采用块参数来指定下载完成后要执行的操作。请参阅AFNetworking作为如何结合块和 NSOperation 子类的一个很好的例子。

As a final observation, you describe this "use blocks" and NSOperation-subclass as an "either/or" proposition. Frequently, though, you actually marry the two techniques, creating an NSOperation subclass that takes block parameters that specify what you want to do when the download is done. See AFNetworking as a wonderful example of how to marry blocks and NSOperation subclass.

这篇关于我可以使用addOperationWithBlock取消添加到NSOperationQueue的块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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