2016-11-07 6 views
0

コアデータ図で色情報を記述しています。エンティティはカラーであり、属性はカラーコンポーネントです。managedObjectを削除する方法 - テーブルビューではありません

私は、グラフからカラーオブジェクトを削除する方法と、次に、(ボーナスの質問?)という2つの面で苦労しています。私AppDelegateで

、私はこのようなコア・データ・スタックがあります。

lazy var persistentContainer: NSPersistentContainer = { 

     let container = NSPersistentContainer(name: "DD") 
     container.loadPersistentStores(completionHandler: { (storeDescription, error) in 
      if let error = error as NSError? { 
       // Replacing this implementation with code to handle the error appropriately. 

       fatalError("Unresolved error \(error), \(error.userInfo)") 
      } 
     }) 
     return container 
    }() 

    // MARK: - Core Data Saving support 

    func saveContext() { 
     print(#function) 
     let context = persistentContainer.viewContext 
     if context.hasChanges { 
      do { 
       try context.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)") 
      } 
     } 
    } 

と私はこれを持って、色を削除しようとしている:

func deleteColor(_ sender:UIButton) { 

     let i : Int = (sender.layer.value(forKey: "index")) as! Int 
     print(#function, "object to delete: ", i) 

     let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

     colors.remove(at: i) 

     do { 
      try managedContext.save() 
     } catch let error as NSError { 
      print("Error While Saving Data: \(error.userInfo)") 
     } 

     recentColorCollection!.reloadData()  
    } 

変数は以下のとおりです。

var colors = [RecentColorEntity]() 
    var colorEntity = "RecentColorEntity" 

エラーは発生していませんが、オブジェクトは削除されていません。私は間違っています

答えて

1
colors.remove(at: i) 

メモリ内のカラー配列から色を削除するだけです。実際のオブジェクトを削除する必要があります。

context.delete(colorObject) 

保存する必要があります。

+0

ありがとうございます!それはトリックでした!私は二重引用符(余分な信用..)を識別する方法に関するあなたのアイデアを歓迎するお返事ありがとう –

+0

あなたは新しい質問を投稿します。 – Mundi

+0

となります。再度、感謝します。 –

関連する問題