How to stop the current NSOperation?

I use NSOperationQueue and NSOperation to run some function on a background click. But I want the user to stop the operation by pressing a button.

How can i do this?

Something like [currentoperation stop];
Cancel - will not work. I want to stop immediately.

thanks

+4
source share
3 answers

You must call the -cancel method, and the operation itself must support cancellation by monitoring the / tpath> t21> property and stopping safely when its value becomes YES . If NSOperation is your own, you may have to create your own subclass to implement this function. You cannot (safely) force an arbitrary operation to stop immediately. It must support cancellation.

+13
source

You cannot stop right away using everything that Apple provides with NSOperation . You can use -[cancel] , as other people suggested, but the current operation will be performed until completion. One way to get closer to using -[isCancelled] inside your operation and sprinkle it throughout the code (especially in long loops). Sort of:

 - (void)main { // do a little work if ([self isCancelled]) { return; } // do a little more work if ([self isCancelled]) { return; } } 

So you will stop soon.

If you really want to stop the flow, you may need to study signal processing. A threaded example is shown here. By sending a custom signal to a specific stream, you can somehow end this stream. It will be a lot more work, and probably a lot more problems than it costs.

+7
source

you use cancel and check if self ( NSOperation ) was canceled at runtime.

+1
source

All Articles