我想知道我是否正确地实现了下面的方法,因为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];
}发布于 2013-01-23 23:40:31
发现问题后,必须更换:
__unsafe_unretained NSBlockOperation *weakOperation = operation;通过以下方式:
__block NSBlockOperation *weakOperation = operation;顺便说一句,对于感兴趣的人,有一个关于并发的很好的视频,特别是在一个单独的线程上绘制并使用WWDC2012中的NSOperationQueue的名为在IOS上构建并发用户界面。
https://stackoverflow.com/questions/14432029
复制相似问题