如何从 NSOperationQueue 中删除/取消 NSInitationOperation?
以下两个问题都是在维护 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
操作完成后会自动从队列中删除,即 -isFinished 返回 true。
当您想中途停止操作时,必须向其发送 -取消消息。 但是这不会神奇地停止操作的运行。您的任务需要定期检查它是否被取消,如果被取消则自行完成。因此,您需要分块下载视频,并在每个块后检查操作的取消状态。下面的伪代码可能会有所帮助:
这意味着,例如,您不能使用
NSURLConnection
的-sendSynchronousRequest:returningResponse:error:
来获取数据,因为它不会能够检查操作的取消状态,直到所有数据都已下载。The operation is automatically removed from the queue when it has finished i.e. when -isFinished returns true.
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:
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.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];