I have this code to manage a collectionview
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [images count];
}
- (PhotoCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoCell *cell = [gridPhoto dequeueReusableCellWithReuseIdentifier:@"photocell" forIndexPath:indexPath];
NSMutableDictionary *record = [images objectAtIndex:[indexPath row]];
if ([record valueForKey:@"actualImage"]) {
[cell.image setImage:[record valueForKey:@"actualImage"]];
[cell.activity stopAnimating];
} else {
dispatch_async(imageQueue_, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[record objectForKey:@"url"]]];
dispatch_async(dispatch_get_main_queue(), ^{
[record setValue:[UIImage imageWithData:imageData] forKey:@"actualImage"];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
});
});
}
return cell;
}
It work fine if the imageQueue_ finish its work and populate the entire collectionview; I have a problem when I exit from my view calling a back
- (IBAction)back:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
In this case if the collectionview is not entire populated of images I have this error when I do back action:
[PhotoGalleryViewController numberOfSectionsInCollectionView:]: message sent to deallocated instance 0x1590aab0
where is the problem?
A download is still in progress when you pop your view controller. So when the asynchronous callback is executed, your [collectionView reloadItemsAtIndexPaths...]
gets called on a collectionView
that no longer exists.
You should check that collectionView != nil
on the first line of your callback block, and simply return;
if it's nil:
dispatch_async(dispatch_get_main_queue(), ^{
if (collectionView == nil)
return;
[record setValue:[UIImage imageWithData:imageData] forKey:@"actualImage"];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
});