Exactly when NSOperation is removed from NSOperationQueue on cancelling request?

I was going through some documents explaining how to manage NSOperation inside NSOperationQueue. My focus is to always do not execute the operation at all if the user pressed a cancel button in a progress panel or quit the application. And thus, cancel the operation to prevent it from consuming CPU time needlessly.

So, whenever I need to cancel operation I should fire cancel method to prevent further execution. Then I will have to use operation object state isCancelled at regular intervals to check whether operation was cancelled. Here are my questions regarding this:

(1) On cancelling request, if NSOperation is being removed from NSOperationQueue, then how do we have still reference to that NSOperation and its property isCancelled ?

According to Apple Developer Class Reference: "NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task."

(2) If I am using ARC, should I need to care about cancelling request ? I am giving example. I have 2 view controllers A and B. In B, I make 8 to 10 NSURLRequest using NSOperation and put all requests in NSOperationQueue. Here object of NSOperationQueue is the property of view controller B. So, if user press back button to get back to view A, under ARC, object of NSOperationQueue should automatically removed (as I pop to view A). Will all operations be cancelled by ARC mechanism, or still I should have some mechanism to avoid useless execution ?

Correct me if I am wrong. Answer and suggestion of this question will surely help me to develop Apps with best performance.

Appreciate your advice. Thanks in advance.


First, an operation is removed from the Queue only when its isFinished property becomes true.

Secondly, if you dealloc B, then the Queue will be dealloced and the operations as well. But you should make sure in your code that you are not referencing these operation objects or the Queue at a later point of time.

链接地址: http://www.djcxy.com/p/65130.html

上一篇: 序列化一个可空的int

下一篇: 恰好当NSOperation从NSOperationQueue中删除请求时被删除?