5

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 ?

halfer
  • 19,824
  • 17
  • 99
  • 186
NSPratik
  • 4,714
  • 7
  • 51
  • 81

1 Answers1

7

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.

devluv
  • 2,007
  • 13
  • 13
  • 1
    _isCancelled_ method return YES if the operation was explicitly cancelled by an invocation of the receiver’s cancel method. So, if I cancel the request, its state goes to _isFinished_ TRUE. Then my question number (1) is still standing... – NSPratik Sep 17 '14 at 06:46
  • `So, if I cancel the request, its state goes to isFinished TRUE`, where did you get this? it is true only for un-queued operations. Please check here https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperation/isCancelled check in the `Responding to the Cancel Command` section. And also the Table above it showing the KeyPath and description. It explicitly states that `an operation queue does not dequeue an operation until the isFinished method returns YES` – devluv Sep 17 '14 at 06:52
  • Sorry croyneaus4u, I was wrong. That statement can only be applied to un-queued operation. Now, my question number 1 is cleared. Thanks a lot royneaus4u. – NSPratik Sep 17 '14 at 07:04