Possible Duplicate:
How do I kill/suspend/close an asyncronous block in GCD?
I am working on an app that does image processing and displays the resulting image. Im using UIScrollView to let user scroll all images, because the image is not a standard jpg or png, it takes time to load. I use GCD to load asynchronously, when finished dispatch to main queue to display. the snippet is as follows:
- (void)loadImage:(NSString *)name
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [Reader loadImage:name];
dispatch_sync(dispatch_get_main_queue(), ^{
[self displayImage:image];
});
});
}
This works well in most cases. But when you scroll so fast that the method may be called several times before the first loaded image to be displayed.Then when you stop,the current imageView will display several previous images quickly, then the current image is displayed at last. which is easy to crash because of memory problem.
I wonder whether there is a way to notify the queue to cancel the previous ones if there is a new block in the queue(which means the method is called again before the previous block is finished)? or any other better suggestions?
Thanks in advance.