【问题标题】:Correct way to update Core Data更新核心数据的正确方法
【发布时间】:2015-01-20 16:01:28
【问题描述】:

我正在尝试在按下按钮时更新我的​​核心数据堆栈。当应用程序在一个会话中运行时,一切看起来都很好,但是当我重新启动应用程序时,我所做的更新不再存在。

我的代码如下所示:

if let indexPath = self.tv.indexPathForRowAtPoint(point){
    let data = messageList[indexPath.row] as Messages
    if data.read == true{
        println("Message already read")
    }else{
        data.read = true
    }
 }

为什么会这样?当我的应用重新启动时,更改总是会恢复。

【问题讨论】:

    标签: ios uitableview swift core-data nsmanagedobject


    【解决方案1】:

    您必须确保在 NSManagedObjectContext 实例上调用 saveAppDelegate 中至少应该已经定义了 on call,但您可能希望更频繁地调用它,例如当您的应用进入后台或类似场合时。

    如需进一步参考,请查看iOS Core Data when to save context?

    【讨论】:

      【解决方案2】:

      您需要在关闭应用程序之前保存 NSManagedObjectContext。

      例如: AppDelegate.swift

      func applicationWillTerminate(application: UIApplication) {
          self.saveContext()
      }
      

      这只有在你杀死应用程序时才有效。

      但是如果你想在 NSManagedObject 发生任何变化后保存上下文,你需要调用 mangedObjectContext.save(&error)

      【讨论】:

      • 谢谢,我完全忘记了! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多