Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 997
Earl Claridad Avatar asked Apr 18 '09 06:04

Earl Claridad


People also ask

How do I cancel NSOperation?

you use cancel , and test whether self (the NSOperation ) has been cancelled during execution.

What is Nsoperationqueue?

A queue that regulates the execution of operations.


2 Answers

UPDATE: Instruments shows leaks-a-plenty when I do this. Proceed with caution! I'm keeping this here in case I'm actually on to something and someone else can figure out how to get over the leak hurdle.

Here's a twisted idea, which I'm re-trying as I type this:

Set the operation as the object for NSInvocationOperation's initWithTarget:selector:object: method. Presuming you already have a NSOperationQueue (we'll call it queue):

NSInvocationOperation *operation = [NSInvocationOperation alloc];
operation = [operation initWithTarget:self selector:@selector(myOperation:) object:operation];
[queue addOperation:operation];
[operation release];

Note that we have to break apart the alloc into its own call. Otherwise we can't set object to operation!

Then, within your operation method, cast the object back and sprinkle checks for isCancelled as desired. For example:

  - (void)myOperation:(id)object {
    NSInvocationOperation *operation = (NSInvocationOperation *)object;
    if ([operation isCancelled]) return;
    ...
  }

Make sure your selector ends with a colon back in the initWithTarget:... call, since you'll be passing in an object now.

So far, so good. Now if I can force cancelAllOperations, I'll know if this actually works. :)

like image 128
Joe D'Andrea Avatar answered Oct 16 '22 22:10

Joe D'Andrea


You need to check if NSInvocationOperation isCancelled is YES. To do that in NSInvocationOperation, you can use Key Value Observing:

Add your object as NSInvocationOperation isCancelled observer when running the operation:

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:<targetObj> selector:@selector(<targetMethod>) object:nil];
[operation addObserver:<targetObj> forKeyPath:@"isCancelled" options:NSKeyValueObservingOptionNew context:nil];
[operQueue addOperation:operation];
[operation release];

Then in targetObj implement

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

to watch for isCancelled being changed by NSOperationQueue's cancellAllOperations. You can set a private flag here and targetMethod can check it and cancel if needed.

like image 34
komorian Avatar answered Oct 17 '22 00:10

komorian