如何取消NSOperationQueue [英] How to cancel NSOperationQueue

查看:289
本文介绍了如何取消NSOperationQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如果我正确实现下面的方法,因为 isCancelled 不取消线程。我有一个图像,我缩放,当它完成缩放,这个方法被调用来更新图像。因此,当用户将其手指抬离按钮时,这被称为。如果他们尝试在完成之前再次按下按钮,我在队列上调用 cancelAllOperations ,但它不工作。甚至不确定cancelAllOperations是否触发标志,或者如果我需要继续调用它来获得结果。

Wondering if I'm implementing the below method correctly because isCancelled is not canceling the thread. I have an image that I'm scaling, and when it's done scaling, this method is called to update the image. So when the user lifts their finger off the button, this is called. If they try to push the button again before this is finished, I call cancelAllOperations on the queue but it's not working. Not even sure if cancelAllOperations triggers a flag, or if I need to keep calling it to get a result. Anyone have any insight on this?

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

BTW,任何感兴趣的人都有一个好的视频并发,特别是绘制一个单独的线程和使用 NSOperationQueue 在WWDC2012中称为在IOS上构建并发用户界面。

BTW, for anyone interested there is a good video on concurrency, in particular drawing on a separate thread and using NSOperationQueue's in WWDC2012 called Building Concurrent User Interfaces on IOS.

这篇关于如何取消NSOperationQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆