Search code examples
iosnsoperation

NSOperation dealloc not called when cancel operation


I am using NSOperation in my application. I am cancelling the previously executing operation when create another operation. But the previously created operation's dealloc method not calling when cancelling that operation.

Pls suggest me. Thanks.


Solution

  • That's just fine:

    Responding to the Cancel Command

    Once you add an operation to a queue, the operation is out of your hands. The queue takes over and handles the scheduling of that task. However, if you decide later that you do not want to execute the operation after all—because the user pressed a cancel button in a progress panel or quit the application, for example—you can cancel the operation to prevent it from consuming CPU time needlessly. You do this by calling the cancel method of the operation object itself or by calling the cancelAllOperations method of the NSOperationQueue class.

    Canceling an operation does not immediately force it to stop what it is doing. Although respecting the value returned by the isCancelled is expected of all operations, your code must explicitly check the value returned by this method and abort as needed. The default implementation of NSOperation does include checks for cancellation. For example, if you cancel an operation before its start method is called, the start method exits without starting the task.

    The dealloc method will be called when the retain count of the object gets to zero alas when no other object is using it.