Skip to content

Instantly share code, notes, and snippets.

@armstrongnate
Last active August 29, 2015 14:19
Show Gist options
  • Save armstrongnate/454449ee41ce7a95a353 to your computer and use it in GitHub Desktop.
Save armstrongnate/454449ee41ce7a95a353 to your computer and use it in GitHub Desktop.
UITableViewController with NSFetchedResultsController boilerplate
//
// MyViewController.swift
//
// Created by Nate Armstrong on 4/15/15.
// Copyright (c) 2015 Nate Armstrong. All rights reserved.
//
import UIKit
import CoreData
class MyViewController: UITableViewController {
var context: NSManagedObjectContext!
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("MyEntity", inManagedObjectContext: self.context)
fetchRequest.entity = entity
let sort = NSSortDescriptor(key: "updated_at", ascending: false)
fetchRequest.sortDescriptors = [sort]
let controller = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: self.context,
sectionNameKeyPath: nil,
cacheName: "Root"
)
controller.delegate = self
return controller
}()
override func viewDidLoad() {
super.viewDidLoad()
var error: NSError? = nil
if !fetchedResultsController.performFetch(&error) {
println("fetch error \(error)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
cell.textLabel!.text = "Set text"
}
}
// MARK: - Table view data source
extension ClientsViewController: UITableViewDataSource {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sectionInfo: AnyObject = fetchedResultsController.sections?[section] {
return sectionInfo.numberOfObjects
}
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ClientCell", forIndexPath: indexPath) as! UITableViewCell
configureCell(cell, atIndexPath: indexPath)
return cell
}
}
// MARK: - Fetched results controller delegate
extension ClientsViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
default:
return
}
}
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment