繁体   English   中英

如何取消NSOperationQueue

[英]How to cancel NSOperationQueue

想知道我是否正确实现了以下方法,因为isCancelled没有取消该线程。 我有一个缩放的图像,当它完成缩放时,调用此方法来更新图像。 因此,当用户将手指从按钮上抬起时,会调用此按钮。 如果他们在完成之前尝试再次按下按钮,我会在队列上调用cancelAllOperations但它不起作用。 甚至不确定cancelAllOperations是否触发了标志,或者我是否需要继续调用它来获得结果。 有人对此有任何见解吗?

- (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];
}

发现问题,必须更换:

__unsafe_unretained NSBlockOperation *weakOperation = operation;

有:

__block NSBlockOperation *weakOperation = operation;

顺便说一句,对于任何有兴趣的人来说,有一个很好的并发视频,特别是在一个单独的线程上绘制,并在WWDC2012中使用NSOperationQueue ,称为在IOS上构建并发用户界面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM