Skip to content

Instantly share code, notes, and snippets.

@vamsiac
Created May 2, 2017 13:40
Show Gist options
  • Save vamsiac/7b4522ebf23919ed011d58c991910713 to your computer and use it in GitHub Desktop.
Save vamsiac/7b4522ebf23919ed011d58c991910713 to your computer and use it in GitHub Desktop.
import UIKit
import CoreData
public class FRCDelegate: NSObject, NSFetchedResultsControllerDelegate {
private unowned let tableView: UITableView
init(tableView: UITableView) {
self.tableView = tableView
super.init()
}
public func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.beginUpdates()
}
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
case .delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
default:
return
}
}
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .update:
tableView.reloadRows(at: [indexPath!], with: .fade)
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .move:
tableView.deleteRows(at: [indexPath!], with: .fade)
tableView.insertRows(at: [newIndexPath!], with: .fade)
}
}
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.endUpdates()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment