0

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?

2
  • Are you using the delegate methods of NSFetchedResultsController and if yes, are the methods called at all?
    – vadian
    Commented Jul 28, 2016 at 16:25
  • which methods do you mena. when the code is without alert everything is fine and deleted row will disappear whith fade. When the alert is used the code does not call controllerDidChangeContent and controllerWillChangeContent and controller methods at all.
    – Milligator
    Commented Jul 29, 2016 at 18:34

3 Answers 3

0

tableview will not show the updates untill you dont update it on the ui thread. for that call dispatch_async() to place call to the main queue.

wherever you are deleting (i guess here from your core data entity) just after that write this- dispatch_async(dispatch_get_main_queue) { self.tableView.reloadData() }

p.s. sorry the answer is not well written.i'm using my phone.

0

You need to reload the tableView once you delete the object from managedObjectContext

managedObjectContext.deleteObject(subjectToDelete)
yourTableView.reloadData()

And for animation with delete action

yourTableView.beginUpdates()
managedObjectContext.deleteObject(subjectToDelete)
yourTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
yourTableView.endUpdates()
1
  • When I use the code without alert everything works fine. When I use the code with alert the problem begins.
    – Milligator
    Commented Jul 28, 2016 at 20:35
0

Your are trying to present the confirmDelete controller twice, you are presenting it right after creation and then again after adding the actions. that is why you are getting the error Application tried to present modally an active controller

2
  • I don't understand what you mean by twice. Would you mind explain more?
    – Milligator
    Commented Jul 28, 2016 at 20:35
  • This line: presentViewController(confirmDelete, animated: true, completion: nil) is been called two times in the same method, you are trying to present a controller that is already presented
    – juanjo
    Commented Jul 28, 2016 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.