Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fdstevex/7a782bb864b7b23b8d8a8e2393286fac to your computer and use it in GitHub Desktop.
Save fdstevex/7a782bb864b7b23b8d8a8e2393286fac to your computer and use it in GitHub Desktop.
Changed implementation to use array of raw blocks instead of NSBlockOperation so it's easy to run them on the main thread.
// Properties:
@property (nonatomic) BOOL shouldReloadCollectionView;
@property (strong, nonatomic) NSMutableArray *collectionViewUpdateBlocks;
// NSFetchedResultsController Delegate Methods:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
self.shouldReloadCollectionView = NO;
self.collectionViewUpdateBlocks = [NSMutableArray array];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
__weak UICollectionView *collectionView = self.collectionView;
switch (type) {
case NSFetchedResultsChangeInsert: {
[self.collectionViewUpdateBlocks addObject:^{
[collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}];
break;
}
case NSFetchedResultsChangeDelete: {
[self.collectionViewUpdateBlocks addObject:^{
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}];
break;
}
case NSFetchedResultsChangeUpdate: {
[self.collectionViewUpdateBlocks addObject:^{
[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.collectionViewUpdateBlocks addObject:^{
[collectionView insertItemsAtIndexPaths:@[newIndexPath]];
}];
}
} else {
self.shouldReloadCollectionView = YES;
}
break;
}
case NSFetchedResultsChangeDelete: {
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) {
self.shouldReloadCollectionView = YES;
} else {
[self.collectionViewUpdateBlocks addObject:^{
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
}];
}
break;
}
case NSFetchedResultsChangeUpdate: {
[self.collectionViewUpdateBlocks addObject:^{
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}];
break;
}
case NSFetchedResultsChangeMove: {
[self.collectionViewUpdateBlocks addObject:^{
[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.collectionViewUpdateBlocks enumerateObjectsUsingBlock:^(void (^updateBlock)(void), NSUInteger idx, BOOL * _Nonnull stop) {
updateBlock();
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment