EXC_BAD_ACCESS in [UICollectionView setCollectionViewLayout:]

I have a collection view where I use a custom UICollectionViewFlowLayout . This collection view can upload images locally or over the Internet. If I upload images locally, each navigation works well. If, however, I upload images via the Internet, the screen loads correctly for the first time, but if you click on the image and go to another screen, and then return from this screen (they are all in the navigation controller), there is “bad access”. Does anyone know what could be causing.

In viewDidLoad

 [self.photosCollectionView registerNib:[UINib nibWithNibName:@"PhotoAlbumPhotosCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kReuseIdentifierPhotosAlbumPhotosCollectionViewCell]; 

In the WillAppear View

 [self fetchImagesFromInternetAsynchronously]; self.photosCollectionView.dataSource = self; self.photosCollectionView.delegate = self; self.photosCollectionViewFlowLayout = [UICollectionViewFlowLayout new]; self.photosCollectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; self.photosCollectionViewFlowLayout.minimumInteritemSpacing = 2.0f; self.photosCollectionViewFlowLayout.minimumLineSpacing = 2.0f; CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; CGFloat cellDimension = (screenWidth - 2*self.photosCollectionViewTrailingSpaceConstraint.constant - 3*self.photosCollectionViewFlowLayout.minimumInteritemSpacing)/4 - 1.0; CGSize cellSize = CGSizeMake(cellDimension, cellDimension); [self.photosCollectionViewFlowLayout setItemSize:cellSize]; [self.photosCollectionView setCollectionViewLayout:self.photosCollectionViewFlowLayout]; 

In fetchImagesFromInternetAsynchronously , I do [self.photosCollectionView reloadData] after the images have been extracted.

Here is the stack trace:

 stop reason = EXC_BAD_ACCESS (code=1, address=0x8) frame #0: 0x0000000112da0e09 UIKit`-[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 248 frame #1: 0x0000000112d4f2d3 UIKit`-[UICollectionView _setCollectionViewLayout:animated:isInteractive:completion:] + 1893 frame #2: 0x0000000112d4e86b UIKit`-[UICollectionView setCollectionViewLayout:] + 318 * frame #3: 0x000000010be6b0aa app`-[PhotoAlbumPhotosPickerViewController viewWillAppear:](self=0x00007fd5830d3e00, _cmd="viewWillAppear:", animated=YES) + 1514 at PhotoAlbumPhotosPickerViewController.m:90 frame #4: 0x000000010eec85fd UIKit`-[UIViewController _setViewAppearState:isAnimating:] + 710 frame #5: 0x000000010eec8c98 UIKit`-[UIViewController __viewWillAppear:] + 149 frame #6: 0x000000010eef8a37 UIKit`-[UINavigationController _startCustomTransition:] + 1203 frame #7: 0x000000010ef08cdb UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 712 frame #8: 0x000000010ef09cea UIKit`-[UINavigationController __viewWillLayoutSubviews] + 57 frame #9: 0x000000010f0afc85 UIKit`-[UILayoutContainerView layoutSubviews] + 248 frame #10: 0x000000010ede4e40 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710 
+5
source share
2 answers

Unbelievable!

This is what ultimately worked. And there was even a similar glitch if I added a collection view of the footer that solved this.

Replace

 self.photosCollectionViewFlowLayout = [UICollectionViewFlowLayout new]; 

with

 self.photosCollectionViewFlowLayout = (UICollectionViewFlowLayout*)self.photosCollectionView.collectionViewLayout; 

Or add this to viewWillDisappear (this, however, did not get rid of the collapse of the footer view, so it’s better to use the first approach.

 self.photosCollectionView.delegate = nil; self.photosCollectionView.dataSource = nil; 
+5
source

UIKit objects are not thread safe. Although you have not included the code causing this in your question, it is likely that fetchImagesFromInternetAsynchronously does work on another thread based on the method signature. And from this thread, [self.photosCollectionView reloadData] is called. The collection view should be accessible only from the main stream:

 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.photosCollectionView reloadData]; }]; 
+2
source

Source: https://habr.com/ru/post/1243782/


All Articles