Created
February 4, 2015 13:01
-
-
Save lukasredev/0ce6b782a5428bd17904 to your computer and use it in GitHub Desktop.
UICollectionView w/ NSFetchedResultsController & NSBlockOperation. Idea originated from Blake Watters (https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController/issues/13) Swift Version
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
func controllerWillChangeContent(controller: NSFetchedResultsController) { | |
self.shouldReloadCollectionView = false | |
self.blockOperation = NSBlockOperation() | |
} | |
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { | |
let collectionView = self.collectionView | |
switch type { | |
case NSFetchedResultsChangeType.Insert: | |
self.blockOperation.addExecutionBlock({ | |
collectionView.insertSections( NSIndexSet(index: sectionIndex) ) | |
}) | |
case NSFetchedResultsChangeType.Delete: | |
self.blockOperation.addExecutionBlock({ | |
collectionView.deleteSections( NSIndexSet(index: sectionIndex) ) | |
}) | |
case NSFetchedResultsChangeType.Update: | |
self.blockOperation.addExecutionBlock({ | |
collectionView.reloadSections( NSIndexSet(index: sectionIndex ) ) | |
}) | |
default:() | |
} | |
} | |
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { | |
let collectionView = self.collectionView | |
switch type { | |
case NSFetchedResultsChangeType.Insert: | |
if collectionView.numberOfSections() > 0 { | |
if collectionView.numberOfItemsInSection( newIndexPath!.section ) == 0 { | |
self.shouldReloadCollectionView = true | |
} else { | |
self.blockOperation.addExecutionBlock({ | |
collectionView.insertItemsAtIndexPaths( [newIndexPath!] ) | |
}) | |
} | |
} else { | |
self.shouldReloadCollectionView = true | |
} | |
case NSFetchedResultsChangeType.Delete: | |
if collectionView.numberOfItemsInSection( indexPath!.section ) == 1 { | |
self.shouldReloadCollectionView = true | |
} else { | |
self.blockOperation.addExecutionBlock({ | |
collectionView.deleteItemsAtIndexPaths( [indexPath!] ) | |
}) | |
} | |
case NSFetchedResultsChangeType.Update: | |
self.blockOperation.addExecutionBlock({ | |
collectionView.reloadItemsAtIndexPaths( [indexPath!] ) | |
}) | |
case NSFetchedResultsChangeType.Move: | |
self.blockOperation.addExecutionBlock({ | |
collectionView.moveItemAtIndexPath( indexPath!, toIndexPath: newIndexPath! ) | |
}) | |
default:() | |
} | |
} | |
func controllerDidChangeContent(controller: NSFetchedResultsController) { | |
// 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 ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome.
I had to subclass NSBlockOperation to keep crashes out, though. Also seem to still be working out a few random issues.