我有通过UI删除和编辑核心数据实体的功能。尽管更改在UI中有效,但在返回视图时不会保留这些更改。我相信我已经错过了一些不会导致任何豁免的事情,但是无法弄明白。是否只是在删除函数的末尾添加标准的save throw和catch?除非是这样才能解开错误?要么 !使用
我已将函数的开关案例包括在内:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
workoutDesignerTable.insertRows(at: [indexPath], with: .fade)
}
break;
case .delete:
if let indexPath = indexPath {
workoutDesignerTable.deleteRows(at: [indexPath], with: .fade)
}
break;
case .update:
if let indexPath = indexPath, let cell = workoutDesignerTable.cellForRow(at: indexPath) as? RoutineTableViewCell {
configure(cell, at: indexPath)
}
break;
default:
print("...")
}
}
删除功能
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Fetch Exercise
let UserExercise = fetchedResultsController.object(at: indexPath)
// Delete Exercise
UserExercise.managedObjectContext?.delete(UserExercise)
}
}
答案 0 :(得分:1)
我认为你需要这样的东西
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let UserExercise = fetchedResultsController.managedObjectContext
UserExercise.delete(fetchedResultsController.object(at: indexPath))
do {
try UserExercise.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}