I am using PageViewController
where I may have more than one image as a content. I am getting image as follows from the Service. However, when user clicks to dismiss the viewcontroller while network operation still going on, app crashes.
queue = [[NSOperationQueue alloc] init];
operation = [NSBlockOperation blockOperationWithBlock:^{
[self addAllImages];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self pageViewcontrollerSetup];
});
}];
[queue addOperation:operation];
}
- (void) addAllImages
{
self.pageImages =[[NSMutableArray alloc] initWithCapacity:self.pageControl.numberOfPages];
for(id key in [[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"]) {
NSString *productURL = [NSString stringWithFormat:@"%@%@", PRODUCT_URL, [[[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"] objectForKey:key]];
NSData* productData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productURL]];
if ([UIImage imageWithData:productData]) {
[self.pageImages addObject:[UIImage imageWithData:productData]];
}
}
}
- (void)closeBtnClicked {
[queue cancelAllOperations];
}
NOTE : I was using GCD
(Grand Central Dispatch) for multi-threading, but I came to know that you cannot cancel it, therefore I swicthed to NSOperationQueue