Skip to content

Instantly share code, notes, and snippets.

@lukasredev
Created February 4, 2015 13:01
Show Gist options
  • Save lukasredev/0ce6b782a5428bd17904 to your computer and use it in GitHub Desktop.
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
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 )
}
}
@Nemesisprime
Copy link

This is awesome.

I had to subclass NSBlockOperation to keep crashes out, though. Also seem to still be working out a few random issues.

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