2013-01-21 91 views
3

想知道如果我正确地执行下面的方法,因为isCancelled不取消线程。我有一个缩放的图像,当缩放完成后,会调用此方法更新图像。所以当用户将他们的手指从按钮上移开时,这就是所谓的。如果他们在完成之前尝试再次按下按钮,我会在队列上呼叫cancelAllOperations,但它不起作用。甚至不知道如果cancelAllOperations触发一个标志,或者如果我需要继续调用它来获得结果。任何人都有这方面的见解?如何取消NSOperationQueue

- (void) refreshImage 
{ 
    NSBlockOperation *operation = [[NSBlockOperation alloc] init]; 
    __unsafe_unretained NSBlockOperation *weakOperation = operation; 

    [operation addExecutionBlock: 
    ^{ 
     UIImage *image = [[self.graphicLayer imageForSize:self.size] retain]; 
     if (![weakOperation isCancelled]) 
     { 
      [[NSOperationQueue mainQueue] addOperationWithBlock: 
       ^{ 
        self.image = image; 
        [image release]; 
       }]; 
     } 
     else 
     { 
      [image release]; 
      return; 
     } 
    }]; 

    [self.queue addOperation: operation]; 
    [operation release]; 
} 
+1

确定在执行if(![weakOperation isCancelled])之前调用'cancelAllOperations'吗? – iDev

+0

我真的不知道是诚实的LOL,我试图弄清楚的事情之一@ACB –

+0

把NSLog语句,并检查哪一个是先调用。 – iDev

回答

1

发现了问题,需要更换:

__unsafe_unretained NSBlockOperation *weakOperation = operation; 

有:

__block NSBlockOperation *weakOperation = operation; 

BTW,有兴趣的人士有上并发好的视频,尤其是图中一个单独的线程并在WWDC2012中使用NSOperationQueue在IOS上调用Building Concurrent User Interfaces。

+0

'__block'应该是合适的,因为你总是保持计数。看到我的答案你的其他帖子http://stackoverflow.com/questions/14484964/substitution-for-weak-when-not-using-arc/14485227#14485227 –

+0

@ s.bandara - 是的,看到它,我更新了这个反应也是如此。谢谢! –

相关问题