Reputation: 21
I am using Dispatch Queue for loading the images but I don't know how to stop the queue if I don't want it to load all the images at some point.
- (void) loadImageInBackgroundArr1:(NSMutableArray *)urlAndTagReference {
myQueue1 = dispatch_queue_create("com.razeware.imagegrabber.bgqueue", NULL);
for (int seriesCount = 0; seriesCount <[seriesIDArr count]; seriesCount++) {
for (int i = 0; i < [urlAndTagReference count]; i++) {
dispatch_async(myQueue1, ^(void) {
NSData *data0 = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[urlAndTagReference objectAtIndex:i]]];
UIImage *image = [[UIImage alloc]initWithData:data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
for (UIView * view in [OD_ImagesScrollView subviews]) {
if ([view isKindOfClass:[UIView class]]) {
for (id btn in [view subviews]) {
if ([btn isKindOfClass:[UIButton class]]) {
UIButton *imagesBtn = (UIButton *)btn;
for (UIActivityIndicatorView *view in [imagesBtn subviews]) {
if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
if (imagesBtn.tag == seriesCount*100+100+i) {
if (view.tag == seriesCount*100+100+i) {
[imagesBtn setImage:image forState:UIControlStateNormal];
[view stopAnimating];
[view removeFromSuperview];
view = nil;
imagesBtn = nil;
}
}
}
}
}
}
}
}
});
[image release];
image = nil;
[data0 release];
data0 = nil;
});
}
}
}
Upvotes: 0
Views: 3072
Reputation: 385580
Dispatch queues don't support cancellation. Two obvious options:
Keep your own cancellation flag as an instance variable, and make the block check the flag before proceeding with its work. All the blocks will end up running, but the blocks that run after you set the cancellation flag will return very quickly.
Use an NSOperationQueue
instead of a dispatch queue. Operation queues support cancelling. If you cancel an operation before it has started running, it will not be run at all. If the operation is already running, it won't be interrupted but you can make it check its isCancelled
flag and return early.
Upvotes: 7