-
-
Save iwasrobbed/5528897 to your computer and use it in GitHub Desktop.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller | |
{ | |
self.shouldReloadCollectionView = NO; | |
self.blockOperation = [[NSBlockOperation alloc] init]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo | |
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type | |
{ | |
__weak UICollectionView *collectionView = self.collectionView; | |
switch (type) { | |
case NSFetchedResultsChangeInsert: { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
}]; | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath | |
{ | |
__weak UICollectionView *collectionView = self.collectionView; | |
switch (type) { | |
case NSFetchedResultsChangeInsert: { | |
if ([self.collectionView numberOfSections] > 0) { | |
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) { | |
self.shouldReloadCollectionView = YES; | |
} else { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView insertItemsAtIndexPaths:@[newIndexPath]]; | |
}]; | |
} | |
} else { | |
self.shouldReloadCollectionView = YES; | |
} | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) { | |
self.shouldReloadCollectionView = YES; | |
} else { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView deleteItemsAtIndexPaths:@[indexPath]]; | |
}]; | |
} | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView reloadItemsAtIndexPaths:@[indexPath]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeMove: { | |
[self.blockOperation addExecutionBlock:^{ | |
[collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath]; | |
}]; | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
// Checks if we should reload the collection view to fix a bug @ http://openradar.appspot.com/12954582 | |
if (self.shouldReloadCollectionView) { | |
[self.collectionView reloadData]; | |
} else { | |
[self.collectionView performBatchUpdates:^{ | |
[self.blockOperation start]; | |
} completion:nil]; | |
} | |
} |
@MatejBalantic you're correct about using [[NSOperationQueue currentQueue] addOperation:self.blockOperation];
v.s. [self.blockOperation start];
. If I use blockOperation.start
, NSFetchedResultsChangeUpdate
events aren't always processed properly (or at all).
Here's an updated controllerDidChangeContent
method that works for me:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
// Checks if we should reload the collection view to fix a bug @ http://openradar.appspot.com/12954582
if (self.shouldReloadCollectionView) {
[self.collectionView reloadData];
} else {
[self.collectionView performBatchUpdates:^{
[[NSOperationQueue currentQueue] addOperation:self.blockOperation];
} completion:nil];
}
}
Thanks.
Hi guys, can anyone tell whats the status of the bug http://openradar.appspot.com/12954582 ?
Do we still have to use reloadData
on insertion of first element to collection views in iOS 9+ ?
I was getting errors from the thread sanitizer that blocks were running off the main thread. Changed to use an array of raw blocks and calling them directly rather than running them through an NSBlockOperation.
https://gist.github.com/fdstevex/7a782bb864b7b23b8d8a8e2393286fac
OK. scrap that ...