How to undo NSOperationQueue

I wonder if I use the method below correctly because isCancelled does not cancel the stream. I have an image that I am scaling, and when it is scaled, this method is called to update the image. Therefore, when the user lifts his finger from the button, it is called. If they try to click the button again until it ends, I call cancelAllOperations in the queue, but it does not work. I'm not even sure if cancelAllOperations calls the flag, or if I need to keep calling to get the result. Does anyone know about 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]; } 
+3
source share
1 answer

Found a problem, you need to replace:

 __unsafe_unretained NSBlockOperation *weakOperation = operation; 

with:

 __block NSBlockOperation *weakOperation = operation; 

BTW, for anyone interested, there is a good video on concurrency, in particular, relying on a separate stream and using the NSOperationQueue in WWDC2012, called Building Concurrent User Interfaces on IOS.

+1
source

All Articles