Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Modify switch statement to ensure correct index paths are returned fo…
Browse files Browse the repository at this point in the history
…r a given change

An insert should have a newIndexPath, a delete should have an older indexPath etc.

This way NSIndexPath? instances aren't being forcibly unwrapped.
  • Loading branch information
rcedwards committed Nov 30, 2015
1 parent 81f5a5c commit 10b1cfe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
54 changes: 33 additions & 21 deletions CoreDataStack/FetchedResultsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,31 +215,43 @@ public class FetchedResultsController<T: NSManagedObject where T: CoreDataModela

// Note: Normally these methods would be put in an extension, but @objc methods cannot be added to generic classes in an extension.

@objc public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
let object = anObject as! T

let change: FetchedResultsObjectChange<T>
switch type {
case .Insert: change = .Insert(object: object, indexPath: newIndexPath!)
case .Delete: change = .Delete(object: object, indexPath: indexPath!)
case .Move: change = .Move(object: object, fromIndexPath: indexPath!, toIndexPath: newIndexPath!)
case .Update: change = .Update(object: object, indexPath: indexPath!)
}

delegate?.didChangeObject(self, change)
@objc public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

let object = anObject as! T

let change: FetchedResultsObjectChange<T>
switch (type, indexPath, newIndexPath) {
case let (.Insert, .None, indexPath?):
change = .Insert(object: object, indexPath: indexPath)

This comment has been minimized.

Copy link
@zwaldowski

zwaldowski Dec 1, 2015

Contributor

We need to handle case (.Insert, .Some, _) because of a bug on iOS 8.0; it's invalid state it happens. See here.

case let (.Delete, indexPath?, .None):
change = .Delete(object: object, indexPath: indexPath)
case let (.Update, indexPath?, .None):
change = .Update(object: object, indexPath: indexPath)
case let (.Move, fromIndexPath?, toIndexPath?):
change = .Move(object: object, fromIndexPath: fromIndexPath, toIndexPath: toIndexPath)
default:
preconditionFailure("Invalid update. Missing a required index path for corresponding change type.")
break
}

delegate?.didChangeObject(self, change)

This comment has been minimized.

Copy link
@zwaldowski

zwaldowski Dec 1, 2015

Contributor

If we're going to use the convenience syntax for .Some(foo), then we might as well use nil over .None.

}

@objc public func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
let info = FetchedResultsSectionInfo<T>(sectionInfo)
@objc public func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType) {

let change: FetchedResultsSectionChange<T>
switch type {
case .Insert: change = .Insert(info: info, index: sectionIndex)
case .Delete: change = .Delete(info: info, index: sectionIndex)
case .Move, .Update: fatalError("Invalid section change type reported by NSFetchedResultsController")
}
let info = FetchedResultsSectionInfo<T>(sectionInfo)

delegate?.didChangeSection(self, change)
let change: FetchedResultsSectionChange<T>
switch type {
case .Insert: change = .Insert(info: info, index: sectionIndex)
case .Delete: change = .Delete(info: info, index: sectionIndex)
case .Move, .Update: preconditionFailure("Invalid section change type reported by NSFetchedResultsController")
}

delegate?.didChangeSection(self, change)
}

@objc public func controllerWillChangeContent(controller: NSFetchedResultsController) {
Expand Down
2 changes: 1 addition & 1 deletion Example/BooksTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BooksTableViewController: UITableViewController {
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
preconditionFailure("init(coder:) has not been implemented")
}

override func viewDidLoad() {
Expand Down

0 comments on commit 10b1cfe

Please sign in to comment.