Present a a UIAlertAction when tableView commit is called and an CoreData entry is deleted

Hi,


I am using Core Data and simply wanted to present a UIAlertController to the screen so that the user can confirm whether or not they wish to go ahead with the deletion of the tableview entry. I have a seperate DataSource class, with the following function implemented:-


    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            guard let deletionItem = students?[indexPath.row] else { return }
            
            let userDeleteAlert = UIAlertController(title: "User Deletion", message: "If you delete this user, it will remove all user information and cannot be retrieved. Do you wish to continue?", preferredStyle: .alert)
            userDeleteAlert.addAction(UIAlertAction(title: "Continue", style: .destructive, handler: nil))
            userDeleteAlert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
            
            // WHAT DO I DO HERE TO PRESENT THE UIALERTCONTROLLER TO THE SCREEN?

            managedObjectContext?.delete(deletionItem)
            do {
                try managedObjectContext?.save()
                students?.remove(at: indexPath.row)
            } catch {
                print("Couldn't save the the CoreData deletion - Student Removal")
            }
            
            tableView.deleteRows(at: [indexPath], with: .fade)
            
        }
        

    }

The question is, on line 9, I cannot simply do something like self.present(userDeleteAlert) as this is not a subclass of a UIViewController. So can anyone explain how I can display the userDeleteAlert?


Best regards.

Answered by OOPer in 421491022

You may need to have a property holding the current view controller:

class DataSource: NSObject {

    weak var viewController: UIViewController?

    //...
}

And use it to present the alert:

            viewController?.present(userDeleteAlert, animated: true, completion: nil)


Of course, you need to set the property before it is used.

I would put the deletion of record (lines 11 to 19) inside the handler of the continue button.

And present the alert on line 9.

                DispatchQueue.main.async {
                    self.present(userDeleteAlert, animated: true, completion: nil)
                }


In some implementation, I also used semaphore to make sure deletion was completed in datasource before deleting row in tableView.

Accepted Answer

You may need to have a property holding the current view controller:

class DataSource: NSObject {

    weak var viewController: UIViewController?

    //...
}

And use it to present the alert:

            viewController?.present(userDeleteAlert, animated: true, completion: nil)


Of course, you need to set the property before it is used.

Thank you, both reponses were what I needed.

🙈🙉🙊

Present a a UIAlertAction when tableView commit is called and an CoreData entry is deleted
 
 
Q