Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling NSOperation and removing it from queue immediately

So I've been thinking about this for a few days, and I can't seem to find a good way to make it work.

I have an NSOperationQueue that allows only ONE operation at a time. I understand from the documentation :

In OS X v10.6 and later, canceling an operation causes the operation to ignore any dependencies it may have. This behavior makes it possible for the queue to execute the operation’s start method as soon as possible. The start method, in turn, moves the operation to the finished state so that it can be removed from the queue.

That when I send cancel it'll set isCancelled to YES, but my problem is :

Say I have 20 NSOperation in the queue and I just cancelled the 18th it'll stay in the NSOperationQueue until it can run and says it's finished (my NSOperation checks correctly for isCancelled) but as it stays in the queue it also stays in the UITableView which I set dataSource to something like myOperationQueue.operations.

And that's what bothers me, user will click Cancel which will invoke the cancel method on the NSOperation but the operation will still be displayed as it's still in the queue.

I thought about invoking start in the cancel method but it isn't allowed only the queue can start it.

EDIT : I also tried to override isFinished in the cancel:

[self willChangeValueForKey:@"isFinished"];
_isFinished = YES;
[self didChangeValueForKey:@"isFinished"];

It works but it also send start to the next NSOperation in the queue and it can lead to having 2 NSOperation at the same time and I only want one.

like image 991
ItsASecret Avatar asked Dec 15 '25 06:12

ItsASecret


1 Answers

So if I understand correctly, the list of outstanding operations is the data source to your UITableView.

One solution would be to filter the list of operations in order to remove the cancelled ones. The table view's data source would be this filtered list, instead of the raw list of operations.

like image 121
Paul Lalonde Avatar answered Dec 16 '25 21:12

Paul Lalonde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!