Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-curtis/1f1cf19860204bb78e52 to your computer and use it in GitHub Desktop.
Save matt-curtis/1f1cf19860204bb78e52 to your computer and use it in GitHub Desktop.
NSFetchedResultsController & UICollectionView using blocks (Moved Items & Deleted/Inserted Sections Fix)
@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
@matt-curtis
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment