开发者

How to remove/cancel NSInvocationOperation from NSOperationQueue?

开发者 https://www.devze.com 2023-03-23 10:39 出处:网络
Both of the following questions are being asked in context to maintain NSOperationQueue and NSInvocationOperation.

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开发者_高级运维?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号