简体   繁体   中英

Error when removing Entity from CoreData in TableView

I have a table view that I am populating with Entities from CoreData. The issue is when I try to delete an entity from the table view. CoreData seems to be removing and saving the update, but there is an error and 2 warnings being thrown. The error seems to be coming from this line

tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)

This is what the error says:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), 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).

The 2 warnings I see are as follows:

No calls to throwing functions occur within 'try' expression
'catch' block is unreachable because no errors are thrown in 'do' block.

Here is the function that is calling this code:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == UITableViewCellEditingStyle.Delete {

        //1
        let personToRemove = persons![indexPath.row]
        print(personToRemove)

        print(indexPath.row)

        //2
        coreDataStack.context.deleteObject(personToRemove)

        //3
        do {
            try! coreDataStack.saveContext()
        } catch let error as NSError {
            print("Could not save: \(error)")
        }

        //4
        tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

When I comment things out to see where the break down is it seems to be the last line. It seems to be complaining about the number of rows, so maybe I'm missing a key piece of the code when it comes to deleting rows.

Thanks!

The underlying method to save the managed object context catches the error so you can safely remove the whole do - catch block.

To remove an item you have to remove it from the table view data source as well as from the Core Data stack.

I recommend to use this order

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == UITableViewCellEditingStyle.Delete {

        //1
        let personToRemove = persons![indexPath.row]
        persons.removeAtIndex(indexPath.row)
        //4
        tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)

        //2
        coreDataStack.context.deleteObject(personToRemove)

        //3
        coreDataStack.saveContext()
    }
}

I recommend further to declare persons as non optional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM