9

cancelAllOperations() doesn't work on the mainQueue (the cancel() method is not called on the NSOperation object). Am I missing something? I have to iterate through all operations and call the cancel() method to get it work.

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
Alexander
  • 1,495
  • 2
  • 19
  • 24
  • 1
    Pretty sure I've seen `-cancelAllOperations` work just fine. Maybe you need to provide more context. – David Dunham Dec 21 '11 at 17:51
  • failing for me in just the way described, today – Bryan Jul 21 '12 at 12:26
  • I know this is an old question, but you say that you're using the `mainQueue` for `NSURLConnection`. You probably want to use a custom queue for `NSURLConnection`-based operations, not `mainQueue`, anyway (you can use `maxConcurrentOperationCount` to enjoy concurrency while controlling the degree of concurrency). If using delegate-based `NSURLConnection`, you can still schedule the connection on the main queue (or create your own `NSThread` with its own runloop, like AFNetworking does). But you probably want to use custom queue for the operations, themselves. – Rob Jul 15 '14 at 05:55

2 Answers2

6

I can also confirm that cancelAllOperations does not work on [NSOperationQueue mainQueue] (at least on my iOS 5.0 Simulator). Might be intentionally designed like that since it is a shared instance.

My simple workaround is just to subclass NSOperation or NSBlockOperation without overriding anything and then do something like this:

-(void)cancelMyOperationsInMainQueue {    
    for (NSOperation* o in [[NSOperationQueue mainQueue] operations]) {
        if ([o isKindOfClass:[MyOperation class]]) {
            [o cancel];
        }
    }
 }
TheTiger
  • 13,264
  • 3
  • 57
  • 82
Rollin_s
  • 1,983
  • 2
  • 18
  • 18
0

Yeah can also confirm it doesn't call cancel method on the operations, it just sets isCancelled = YES

My solution: [[[NSOperationQueue mainQueue] operations] makeObjectsPerformSelector:@selector(cancel)];

trapper
  • 11,716
  • 7
  • 38
  • 82