I have a similar problem to - Delete Data from Coredata Swift.
I'm trying to delete a row from a table view and I do get the following error:
Terminating app due to uncaught exception NSInternalInconsistencyException" reason - attempt to delete row 0 from section 0 which only contains 0 rows before the update
My code is at it follows:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let context: NSManagedObjectContext = self.fetchedResultsController.managedObjectContext
NSLog("numberOfRowsInSection %d", self.tableView.numberOfRowsInSection(o))
if editingStyle == .Delete
{
let alertController = UIAlertController(title: "Delete menu item", message: "Are you sure you want to delete \((self.fetchedResultsController.fetchedObjects as [Menu])[indexPath.row].menuText)?", preferredStyle: UIAlertControllerStyle.Alert)
let defaultAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
(action: UIAlertAction!) -> Void in
context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject)
var menus = self.fetchedResultsController.fetchedObjects as [Menu]
menus.removeAtIndex(indexPath.row)
context.save(nil)
NSLog("numberOfRowsInSection %d", self.tableView.numberOfRowsInSection(o))
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
tableView.reloadData()
});
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
}
Could anyone provide me some help? Thank you in advance!