Running UICollectionViewBatchUpdates causes the collection to crash when the device rotates

if i rotate the device. While collectionview does BatchUpdate with reloadItemsAtIndexPaths, the collection view will be inappropriate. but the view will still remain in a constant position.

To just fix this problem:

setting [UIView setAnimationEnabled: NO]; in willRotateToInterfaceOrientation + setting [UIView setAnimationEnabled: YES]; in didRotateToInterfaceOrientation 

But I do not think this is a good way to solve this problem.

Has anyone got a better idea?

Greetings.

Here is my code:

  - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; NSLog(@"Before Rotation"); NSLog(@"collecitonview Rect : %@", NSStringFromCGRect(self.collectionView.frame)); NSLog(@"view Rect : %@", NSStringFromCGRect(self.view.frame)); } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; NSLog(@"After Rotation"); NSLog(@"collecitonview Rect : %@", NSStringFromCGRect(self.collectionView.frame)); NSLog(@"view Rect : %@", NSStringFromCGRect(self.view.frame)); } - (void) viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; for(int i = 0 ; i < 30; i++){ NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ CGFloat hue = arc4random() % 20; [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:hue inSection:0]]]; }]; [_operationsArray addObject:operation]; } [self performUpdate]; } - (void)performUpdate{ if(_operationsArray.count == 0)return; [self.collectionView performBatchUpdates:^{ NSBlockOperation *operation = (NSBlockOperation*)[_operationsArray firstObject]; [operation start]; } completion:^(BOOL finished) { if(_operationsArray.count != 0){ [_operationsArray removeObjectAtIndex:0]; [self performUpdate]; } }]; } 

output: (if I turned the device during the update)

 2013-10-16 17:05:18.445 collectionviewbatchupdateTest[90419:a0b] Before Rotation 2013-10-16 17:05:18.446 collectionviewbatchupdateTest[90419:a0b] collecitonview Rect : {{0, 0}, {1024, 768}} 2013-10-16 17:05:18.446 collectionviewbatchupdateTest[90419:a0b] view Rect : {{0, 0}, {768, 1024}} 2013-10-16 17:05:18.851 collectionviewbatchupdateTest[90419:a0b] After Rotation 2013-10-16 17:05:18.851 collectionviewbatchupdateTest[90419:a0b] collecitonview Rect : {{-128, 0}, {1024, 1024}} 2013-10-16 17:05:18.852 collectionviewbatchupdateTest[90419:a0b] view Rect : {{0, 0}, {768, 1024}} 
+6
source share
3 answers

Same problem. This seems like a bug in a UICollectionView. The only other workaround I could find was to reset the collection view frame after rotation.

This happens in any package updates during rotation, and not just reloadItemsAtIndexPaths.

0
source

I process this by reapplying the collection viewport after rotation:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { self.collectionView.frame = self.view.bounds; } 
0
source

This seems to be a UICollectionView problem. My suggestion is to set the frame after executing the execution.

 CGRect frame = self.collectionView.frame; [self.collectionView performBatchUpdates:^{ NSBlockOperation *operation = (NSBlockOperation*)[_operationsArray firstObject]; [operation start]; } completion:^(BOOL finished) { if(_operationsArray.count != 0){ [_operationsArray removeObjectAtIndex:0]; [self performUpdate]; } if(CGRectEqualToRect(self.collectionView.frame, frame)) { [self.collectionView setFrame:frame]; } }]; 
0
source

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


All Articles