ex. Entire code
func loadItems() {
let request: NSFetchRequest<Item> = Item.fetchRequest()
do {
itemArray = try context.fetch(request)
} catch {
print("Error fetching data from context \(error)")
}
}
// Used do-catch statement as fetch() can return an error
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
itemArray[indexPath.row].isDone = !itemArray[indexPath.row].isDone
saveItems()
tableView.deselectRow(at: indexPath, animated: true) // cell 을 누르자마자 deselect 되는 애니메이션이 나올 수 있도록
}
func saveItems() {
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
self.tableView.reloadData()
}