如何取消NSOperationQueue
想知道如果我正确地执行下面的方法,因为isCancelled
不取消线程。 我有一个图像,我缩放,当它完成缩放,调用此方法来更新图像。 所以当用户把手指从button上拿下时,这就是所谓的。 如果他们在完成之前尝试再次按下button,我会在队列上调用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;
顺便说一句,对于任何感兴趣的人来说,都有一个关于并发的好video,特别是在一个单独的线程上绘图,并且在WWDC2012中使用NSOperationQueue
在IOS上构build并发用户界面。