Created
May 12, 2016 22:21
-
-
Save marcmatta/566afe23e93a16f5909f8944ac063a4d to your computer and use it in GitHub Desktop.
NSFetchedResultsController iOS 8+
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
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