Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
fix #120 - crash when last UITableView row moves out from section 0 (#…
Browse files Browse the repository at this point in the history
…121)

* fix #120 - crash when last UITableView row moves out from section 0

* no need for UICollectionView workaround when using a UITableView

* cover case when `.update` object is called but the object is also moved
  • Loading branch information
yonat authored and 3lvis committed Oct 19, 2019
1 parent cf5e9a8 commit 5ae0260
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions Source/DATASource+NSFetchedResultsControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extension DATASource: NSFetchedResultsControllerDelegate {
break
case .update:
if let newIndexPath = newIndexPath {
if let indexPath = indexPath, indexPath != newIndexPath {
fallthrough
}
if tableView.indexPathsForVisibleRows?.firstIndex(of: newIndexPath) != nil {
if let cell = tableView.cellForRow(at: newIndexPath) {
self.configure(cell, indexPath: newIndexPath)
Expand All @@ -79,24 +82,11 @@ extension DATASource: NSFetchedResultsControllerDelegate {
break
case .move:
if let indexPath = indexPath, let newIndexPath = newIndexPath {
// Workaround: Updating a UICollectionView element sometimes will trigger a .Move change
// where both indexPaths are the same, as a workaround if this happens, DATASource
// will treat this change as an .Update
if indexPath == newIndexPath {
if let cell = tableView.cellForRow(at: newIndexPath) {
self.configure(cell, indexPath: newIndexPath)
}

if let anObject = anObject as? NSManagedObject {
self.delegate?.dataSource?(self, didUpdateObject: anObject, atIndexPath: newIndexPath)
}
} else {
tableView.deleteRows(at: [indexPath], with: rowAnimationType)
tableView.insertRows(at: [newIndexPath], with: rowAnimationType)
tableView.deleteRows(at: [indexPath], with: rowAnimationType)
tableView.insertRows(at: [newIndexPath], with: rowAnimationType)

if let anObject = anObject as? NSManagedObject {
self.delegate?.dataSource?(self, didMoveObject: anObject, fromIndexPath: indexPath, toIndexPath: newIndexPath)
}
if let anObject = anObject as? NSManagedObject {
self.delegate?.dataSource?(self, didMoveObject: anObject, fromIndexPath: indexPath, toIndexPath: newIndexPath)
}
}
break
Expand Down

0 comments on commit 5ae0260

Please sign in to comment.