3

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.

ItsASecret
  • 2,589
  • 3
  • 19
  • 32

1 Answers1

5

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.

Paul Lalonde
  • 5,020
  • 2
  • 32
  • 35
  • Yeah I think I can do that but I was hoping to solve it without messing with the tableView just the operation and the queue :p If there's no other way I'll do it like that and mark your answer as correct :) But I'm still hoping for another solution :p – ItsASecret Jan 19 '13 at 19:32
  • 2
    @Clarisse: This shouldn't be much more than a one-line change. Instead of populating your table from `[myQueue operations]`, use `[[myQueue operations] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isCancelled == NO"]]` – jscs Jan 19 '13 at 19:48
  • 1
    Omg predicates -.- use it everywhere but couldn't think of it for this situation -.- Thank you very much ! – ItsASecret Jan 19 '13 at 19:49