如何从NSOperationQueue中删除/取消NSInvocationOperation? [英] How to remove/cancel NSInvocationOperation from NSOperationQueue?

查看:402
本文介绍了如何从NSOperationQueue中删除/取消NSInvocationOperation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上下文中要求提出以下两个问题以维护NSOperationQueue和NSInvocationOperation。

Both of the following questions are being asked in context to maintain NSOperationQueue and NSInvocationOperation.

由于我使用此概念下载多个视频,如何在完成下载视频后从NSOperationQueue中删除/释放添加的NSInvocationOperation?

As I have used this concept to download multiple videos, how do I remove/release added NSInvocationOperation from NSOperationQueue after completion of downloading a video?

此外,如果我想在正在下载的过程中停止下载特定视频,我该怎么办?

And also, what should I do if in case I want to stop to download specific video while the process of downloading in progress?

推荐答案


如何在完成下载视频后从NSOperationQueue删除/释放添加的NSInvocationOperation?

how do I remove/release added NSInvocationOperation from NSOperationQueue after completion of downloading a video?

操作在完成后自动从队列中删除,即 - isFinished 返回true。

The operation is automatically removed from the queue when it has finished i.e. when -isFinished returns true.


此外,如果我想在正在下载的过程中停止下载特定视频,我该怎么办?

And also, what should I do if in case I want to stop to download specific video while the process of downloading in progress?

如果要在中途停止某项操作,则必须发送 - 取消消息。 然而这不会神奇地停止运行。您的任务需要定期检查是否已取消,如果事实证明已经完成,则需要自行完成。因此,您需要以块的形式下载视频,并在每个块之后检查操作的取消状态。以下伪代码可能有所帮助:

When you want to stop an operation part way through, you must send it the -cancel message. However this will not magically stop the operation from running. Your task needs to periodically check if it is cancelled and finish itself if it turns out that it has been. So you need to download your video in chunks and check the operation's cancelled status after each chunk. The following pseudocode might help:

 while (![myOperation isCancelled] && thereIsMoreData)
 {
     download a chunk of data and save it
 }

这意味着,例如,你不能使用 NSURLConnection -sendSynchronousRequest:returningResponse:error:来获取数据,因为它不会能够在所有数据都已下载之前检查已取消的操作状态。

This means, for instance, you can't use NSURLConnection's -sendSynchronousRequest:returningResponse:error: to get the data because it won't be able to check the cancelled status of the operation until after all the data is already downloaded.

这篇关于如何从NSOperationQueue中删除/取消NSInvocationOperation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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