Deleting Data from Core Data (Delete in CRUD)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
context.delete(itemArray[indexPath.row])
itemArray.remove(at: indexPath.row)
saveItems()
tableView.deselectRow(at: indexPath, animated: true)
}
- The order which we delete and remove matters a huge deal.
- We first need to delete what we want to delete from the context, or the staging area. Then, we delete the itemArray that are tableview uses as a datasource to populate the cells.
- Then, we want to save the new results to the context, then to the persistent container.
- So, except for Read in CRUD, you would always have to commit your changes after creating, updating, or deleting data by context.save( ).