Search code examples
objective-ccocoamultithreadingpyobjc

How to stop an NSInvocationOperation?


I have an NSInvocationOperation that would download and parse a series of NSXMLDocuments in the background to my UI responsive.

My attempt at stopping the Invocation operation is to call my NSOperationQueue's cancellAllOperations. But it seems that this won't stop the invocation's execution.

Any ideas on how would I go about this problem?


Solution

  • It's up to the implementation of your NSOperation object to actually stop what it's doing, clean up, and exit when it gets notified that it's been cancelled. Messaging that you want to cancel all operations on the queue will cause the queue to stop dequeueing new operations to run and will send the cancel message to any operations currently running.

    In your operation's main method, you should be checking for isCancelled and handling that state when you are actually cancelled.

    For more information, see the Creating and Managing Operation Objects in the Threading Programming Guide.