2017-04-21 8 views
0

私のアプリからSwift 3とXcode 8のエンティティを更新したいと思います。iOS - CoreDataModelエンティティを更新するにはどうすればよいですか?

コアデータモデルを使用しました。

これは私のCoreDataController.swiftのコードです:

このモードで
// Updates a person 
    func update(updateLuci: LuciKit){ 
     if let luci = getById(id: updateLuci.objectID){ 
      luci.descrizione = updateLuci.descrizione 
      luci.pin_arduino = updateLuci.pin_arduino 
     } 
    } 

    // Gets a person by id 
    func getById(id: NSManagedObjectID) -> LuciKit? { 
     return context.object(with: id) as? LuciKit 
    } 

私はすべてのエラーを持っていないが、私は自分のアプリケーションを再起動しようとした場合、私は、更新情報を表示することはできません。

+0

? – vadian

答えて

1

私はあなたがコンテキストを保存するために

func update(updateLuci: LuciKit){ 
     if let luci = getById(id: updateLuci.objectID){ 
      luci.descrizione = updateLuci.descrizione 
      luci.pin_arduino = updateLuci.pin_arduino 
      context.save() 
     } 
    } 
0

を忘れてしまったあなたは、このようcontextを保存する必要があります考えて、これを試してください:あなたは、変更を永続化するためにどこかに管理対象オブジェクトコンテキストを保存してください

func update(updateLuci: LuciKit){ 
     if let luci = getById(id: updateLuci.objectID){ 
      luci.descrizione = updateLuci.descrizione 
      luci.pin_arduino = updateLuci.pin_arduino 

      //let delegate = UIApplication.shared.delegate as! AppDelegate 
      //let context = delegate.managedObjectContext 

      do { 
       try context.save() 
      } catch let err { 
       print(err) 
      } 
     } 
    } 

    // Gets a person by id 
    func getById(id: NSManagedObjectID) -> LuciKit? { 
     return context.object(with: id) as? LuciKit 
    } 
関連する問題