How to remove/cancel NSInvocationOperation from NSOperationQueue?

二次信任 提交于 2019-12-03 17:33:52

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.

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];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!