【问题标题】:CoreData save record without contextsave没有 contextsave 的 CoreData 保存记录
【发布时间】:2017-02-14 01:03:28
【问题描述】:

项目是我的 NSManagedObject

 let proj = Project(context: context!)
 //at this point when i try to fetch for my proj nothing is there

 proj.title = "title"
 //at this point I can fetch my project record

现在我试试

let proj = Project(context: context!)
let proj1 = Project(context: context!)
//at this point when i try to fetch for my proj nothing is there

 proj.title =  "title"
 //I find 1 record

我稍微修改一下代码

let proj = Project(context: context!)
let proj1 = Project(context: context!)
//when i try to fetch for my proj nothing is there

 proj.title = "title"
 proj1.title = "title"

 //I find 2 record

好像 proj.title = "title" 正在保存记录

我的子类中没有 .save()

这是为什么?我很困惑

我认为对象仅在您调用时才保存 managedObjectContext.save

【问题讨论】:

    标签: core-data swift3


    【解决方案1】:

    对象在插入 ManagedObjectContext 后立即存在。

    .save() 将更改从 MOC 推送到其父上下文或持久存储。这个想法是您可以在临时 MOC 中操作对象,但在响应“取消”时将其丢弃。

    如果您使用指向同一个持久性存储的不同 MOC 进行提取,您将不会在用于插入的 MOC 上看到没有 .save() 的更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多