2017-02-14 72 views
0

項目是我的NSManagedObjectCoreData保存記錄,而不contextsave

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 =「標題「正在保存記錄

I在我的子類中沒有.save()

這是爲什麼?我很困惑

我以爲對象只保存,當你調用 managedObjectContext.save

回答

1

對象存在的ManagedObjectContext內插入後即可。

.save()將更改從MOC推送到其父上下文或持久性存儲。這個想法是,你可以操縱MOC臨時對象中的對象,但是在響應「取消」時丟棄它們。

如果您使用指向相同持久性存儲的其他MOC執行提取操作,則在用於插入操作的MOC上不會看到沒有.save()的更改。