iOS8 Swift: deleteRowsAtIndexPaths crashes

I am having some trouble with deleting a row from my tableView in Swift, iOS 8, Xcode 6 Beta 6. Every time I try to delete a row I get an error along the lines of

Assertion failure in -[UITableView _endCellAnimationsWithContext:],
/SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-08-30
20:31:00.971 Class Directory[13290:3241692] *** Terminating app due to
uncaught exception ‘NSInternalInconsistencyException’, reason:
‘Invalid update: invalid number of rows in section 1. The number of
rows contained in an existing section after the update (25) must be
equal to the number of rows contained in that section before the
update (25), plus or minus the number of rows inserted or deleted from
that section (0 inserted, 1 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in, 0 moved out).

  • Swift 3 how to resolve NetService IP?
  • How to generate pdf using NSData or using data bytes objective c?
  • iOS development app startup crash
  • Magical Record import (next step)
  • Change the selected TabBar index on button click
  • Passing data between segues without using PrepareForSegue
  • I have read all the answers to this frequent problem, here, and feel I have fulfilled the conditions recommended. The item seems to be removed from the data model — when I reload the app, the deleted item is gone from the table — but there seem to be some remnants in the appropriate sqlite file and of course the math doesn’t add up. The println that spits out the indexPath shows the correct Section and Row. I’m very puzzled. This should be straightforward but I am missing something dumb I am sure, I suspect in the data model deletion. Full project on Github.

    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    
        return fetchedResultController.sections.count
    
    }
    
    
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return fetchedResultController.sections[section].numberOfObjects
    
        }
    
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell = tableViewMain.dequeueReusableCellWithIdentifier("CellMain", forIndexPath: indexPath) as UITableViewCell
    
            let personForRow = fetchedResultController.objectAtIndexPath(indexPath) as Person
            cell.textLabel.text = personForRow.fullName()
    
            return cell
    
    }
    
    func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
        return true
    }
    
    func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.Delete
    }
    
     func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
        println("section and row \(indexPath.section) \(indexPath.row) ")
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
        let personForRow : NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as Person
        context?.deleteObject(personForRow)
        context?.save(nil)
            tableViewMain.beginUpdates()
        tableViewMain.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            tableViewMain.endUpdates()
        }
    

    2 Solutions Collect From Internet About “iOS8 Swift: deleteRowsAtIndexPaths crashes”

    It’s easy to reproduce your crash with a Xcode Core Data Master-Detail template project. As a general rule, when you use NSFetchedResultsController, you should really use NSFetchedResultsControllerDelegate (you have declared it but don’t use it).

    Delete those lines in your tableView:commitEditingStyle:forRowAtIndexPath: method:

    tableViewMain.beginUpdates()
    tableViewMain!.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
    tableViewMain.endUpdates()
    

    And add those lines to your viewController class:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
        tableViewMain.beginUpdates()
    }
    
    func controller(controller: NSFetchedResultsController!, didChangeSection sectionInfo: NSFetchedResultsSectionInfo!, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
        switch type {
        case .Insert:
            tableViewMain.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
        case .Delete:
            tableViewMain.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
        default:
            return
        }
    }
    
    func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
        switch type {
        case .Insert:
            tableViewMain.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
        case .Delete:
            tableViewMain.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        case .Update:
            return
            //Should also manage this case!!!
            //self.configureCell(tableView.cellForRowAtIndexPath(indexPath), atIndexPath: indexPath)
        case .Move:
            tableViewMain.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            tableViewMain.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
        default:
            return
        }
    }
    
    func controllerDidChangeContent(controller: NSFetchedResultsController!) {
        tableViewMain.endUpdates()
    }
    

    This should fix your problem.

    I believe this is simply a caching problem. Your fetchedResultController is not going to automatically refetch your results as it caches its results. That means that when tableView:numberOfRowsInSection: is called again, the results count is still returning 25 even though you deleted an item.