如何从 NSOperationQueue 中删除/取消 NSInitationOperation?

发布于 2024-11-26 11:51:41 字数 196 浏览 3 评论 0原文

以下两个问题都是在维护 NSOperationQueue 和 NSInvocableOperation 的上下文中提出的。

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

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

Both of the following questions are being asked in context to maintain NSOperationQueue and 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-12-03 11:51:41

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

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

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

当您想中途停止操作时,必须向其发送 -取消消息。 但是这不会神奇地停止操作的运行。您的任务需要定期检查它是否被取消,如果被取消则自行完成。因此,您需要分块下载视频,并在每个块后检查操作的取消状态。下面的伪代码可能会有所帮助:

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

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

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

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
 }

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.

深海夜未眠 2024-12-03 11:51:41

NSOperation 执行该任务完成后,它将自动从队列中删除。参考链接

创建 NSOperation 的子类。为每个操作保留一个标识符。将所有操作存储在数组中。当您不想继续某个操作时,只需向该操作发送取消消息即可,即[_operation cancel];

After an NSOperation performed that task to completion, it will be automatically removed from the queue. Ref link.

Make sub-class of NSOperation. Keep an identifier for each operation. Store all the operations in array. When u dont want an operation to continue, just give cancel message to the operation , ie., [_operation cancel];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文