Last active
December 18, 2015 03:48
-
-
Save jrturton/5720249 to your computer and use it in GitHub Desktop.
UICollectionView fed by NSFetchedResultsController
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
#pragma mark - NSFetchedResultsControllerDelegate | |
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { | |
self.pendingObjectUpdates = [NSMutableArray array]; | |
self.pendingSectionUpdates = [NSMutableArray array]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo | |
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { | |
__weak UICollectionView *collectionView = self.collectionView; | |
CollectionViewUpdateBlock updateBlock; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
{ | |
updateBlock = ^(){[collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];}; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: | |
{ | |
updateBlock = ^(){[collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];}; | |
break; | |
} | |
case NSFetchedResultsChangeMove:break; | |
case NSFetchedResultsChangeUpdate:break; | |
} | |
[self.pendingSectionUpdates addObject:updateBlock]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject | |
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type | |
newIndexPath:(NSIndexPath *)newIndexPath { | |
__weak UICollectionView *collectionView = self.collectionView; | |
CollectionViewUpdateBlock updateBlock; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
{ | |
updateBlock = ^{DLog(@"processing insert to %@",newIndexPath); [collectionView insertItemsAtIndexPaths:@[newIndexPath]];}; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: | |
{ | |
updateBlock = ^{DLog(@"processing delete to %@",indexPath); [collectionView deleteItemsAtIndexPaths:@[indexPath]];}; | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: | |
{ | |
updateBlock = ^{DLog(@"processing update to %@",indexPath);[self configureCell:(HLGridTableCell*)[collectionView cellForItemAtIndexPath:indexPath] atIndexPath:indexPath];}; | |
break; | |
} | |
case NSFetchedResultsChangeMove: | |
{ | |
updateBlock = ^{DLog(@"processing move from %@ to %@",indexPath,newIndexPath); [collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];}; | |
break; | |
} | |
} | |
[self.pendingObjectUpdates addObject:updateBlock]; | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
if ([self.pendingSectionUpdates count]) | |
{ | |
[self.collectionView performBatchUpdates:^{ | |
for (CollectionViewUpdateBlock updateBlock in self.pendingSectionUpdates) | |
{ | |
updateBlock(); | |
} | |
} completion:nil]; | |
} | |
if ([self.pendingObjectUpdates count] > 0 && [self.pendingSectionUpdates count] == 0) | |
{ | |
[self.collectionView performBatchUpdates:^{ | |
for (CollectionViewUpdateBlock updateBlock in self.pendingObjectUpdates) | |
updateBlock(); | |
} completion:nil]; | |
} | |
self.pendingSectionUpdates = nil; | |
self.pendingObjectUpdates = nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment