2016-11-30 2 views
0

UIを使用してコアデータエンティティを削除および編集する機能があります。変更はUIで機能しますが、ビューに戻るときは変更されません。私は、何も起こらないセービングを引き起こしたものを逃したと確信していますが、それを理解できません。それは、標準のセーブ・スローを追加して、削除関数の最後にキャッチする場合に過ぎませんか?それは私にアンラッピングエラーを与えない限りですか?または!エンティティを変更するときにコアデータが永続する問題

を使用している私は、関数たとえば、スイッチケースが含まれている:

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に

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) 
    } 
} 

答えて

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)") 
     } 
    } 
} 
+0

のように、この答えを何かが必要だと思いますありがとう! – infernouk

関連する問題