CoreData - 创建新实体 - 为什么不需要保存它?

CoreData - creating new entity - Why don't you need to save this?

我认为您可以像这样创建新实体(在 swift 3 中):

let person = Person(context: persistentContainer.viewContext) person.name = "Some Name"

好像是这样。它永久地保存了新人(至少我是这么认为的)。

为什么不需要调用 AppDelegate 的 saveContext()。 swift(或者persistentContainer.viewContext.save(),基本一样吧?)?

每次更改一些entity,都需要保存。为什么创建新 entities 时不是这种情况?

提前致谢!!!

根据您对问题的评论,您正在致电 saveContext()

进入您的 AppDelegate 并查看 applicationWillTerminate,那里调用了 saveContext()

简而言之,如果你想持久化数据那么是的,你需要调用saveContext()

为了您的回答,您必须了解核心数据堆栈

https://developer.apple.com/library/ios/documentation/DataManagement/Devpedia-CoreData/coreDataStack.html#//apple_ref/doc/uid/TP40010398-CH25-SW1

在保存上下文之前,您对托管对象所做的更改不会提交到父存储。