Last active
August 29, 2015 14:24
-
-
Save matt-curtis/1f1cf19860204bb78e52 to your computer and use it in GitHub Desktop.
NSFetchedResultsController & UICollectionView using blocks (Moved Items & Deleted/Inserted Sections Fix)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface YourNSFetchedResultsControllerDelegate () <NSFetchedResultsControllerDelegate> | |
@end | |
@implementation YourNSFetchedResultsControllerDelegate { | |
UICollectionView *_collectionView; | |
NSMutableArray *_collectionViewUpdatesQueue; | |
} | |
#pragma mark - | |
#pragma mark NSFetchedResultsControllerDelegate methods | |
- (void) controllerWillChangeContent:(NSFetchedResultsController*)controller { | |
_collectionViewUpdatesQueue = [NSMutableArray new]; | |
} | |
- (void) controller:(NSFetchedResultsController*)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { | |
__weak UICollectionView *collectionView = _collectionView; | |
switch(type){ | |
case NSFetchedResultsChangeInsert: { | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: { | |
[_collectionViewUpdatesQueue 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 = _collectionView; | |
switch(type){ | |
case NSFetchedResultsChangeInsert: { | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView insertItemsAtIndexPaths:@[ newIndexPath ]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView deleteItemsAtIndexPaths:@[ indexPath ]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: { | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView reloadItemsAtIndexPaths:@[ indexPath ]]; | |
}]; | |
break; | |
} | |
case NSFetchedResultsChangeMove: { | |
/* | |
This is here to fix a bug(?) with UICollectionView. | |
Basically, it does not like it when you try to move items | |
from a section that will be deleted, or to a section that will be inserted. | |
*/ | |
[_collectionViewUpdatesQueue addObject:^{ | |
[collectionView deleteItemsAtIndexPaths:@[ indexPath ]]; | |
[collectionView insertItemsAtIndexPaths:@[ newIndexPath ]]; | |
}]; | |
break; | |
} | |
default: break; | |
} | |
} | |
- (void) controllerDidChangeContent:(NSFetchedResultsController*)controller { | |
[_collectionView performBatchUpdates:^{ | |
for(void(^updateBlock)() in _collectionViewUpdatesQueue) updateBlock(); | |
} completion:nil]; | |
_collectionViewUpdatesQueue = nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses blocks to queue collection view updates and replaces
[UICollectionView moveItemAtIndexPath:toIndexPath:]
with an deletion and insert in order to fix the (above) mentioned bug.