I use the following code to delete a row from UITableView.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let personToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Person
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
managedObjectContext.deleteObject(personToDelete)
do{
try managedObjectContext.save()
} catch {
print(error)
}
}
}
}
and everything Works fine when I add an alert to the code the deleted row will remain in the tableView.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let personToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Person
let confirmDelete = UIAlertController(title: "Remove Person", message: "Are you sure to delete \"\(personToDelete.name!)\" and all of its data.", preferredStyle: .ActionSheet)
presentViewController(confirmDelete, animated: true, completion: nil)
let deleteAction = UIAlertAction(title: "Delete", style: .Destructive, handler: { (action : UIAlertAction) in
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
managedObjectContext.deleteObject(subjectToDelete)
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action : UIAlertAction) in
})
confirmDelete.addAction(deleteAction)
confirmDelete.addAction(cancelAction)
presentViewController(confirmDelete, animated: true, completion: nil)
}
}
I tried to debug using some break points on beginUpdates and endUpdates. but it seems when I use alert, they won't be called.
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
print("update ended")
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
print("update Started")
}
without the print lines tableView does not have any changes unless closing and opening the view. when i add print lines an error comes
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller
what is the problem?
NSFetchedResultsController
and if yes, are the methods called at all?