Skip to content

Instantly share code, notes, and snippets.

@marcmatta
Created May 12, 2016 22:21
Show Gist options
  • Save marcmatta/566afe23e93a16f5909f8944ac063a4d to your computer and use it in GitHub Desktop.
Save marcmatta/566afe23e93a16f5909f8944ac063a4d to your computer and use it in GitHub Desktop.
NSFetchedResultsController iOS 8+
extension SampleController : NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(controller: NSFetchedResultsController) {
if controller == feedFetchedResultsController {
tableView.beginUpdates()
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
if controller == feedFetchedResultsController {
tableView.endUpdates()
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
if controller == feedFetchedResultsController {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
case .Update:
if let indexPath = indexPath {
let cell = tableView.cellForRowAtIndexPath(indexPath) as? DisposableCell
if let cell = cell {
configureCell(cell, atIndexPath: indexPath)
}
}
case .Move:
if let indexPath = indexPath, let newIndexPath = newIndexPath {
if (!indexPath.isEqual(newIndexPath)){
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}else {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment