SwiftedMind
SwiftedMind

Reputation: 4287

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

I figured you can create new entities (in swift 3) like this:

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

This seems to be it. It saves the new person permanently (I think so, at least).

Why don't you need to call saveContext()of AppDelegate. swift (or persistentContainer.viewContext.save() which is basically the same, right?)?

Every time you change some entity, you need to save it. Why isn't this the case when creating new entities?

Thanks in Advance !!!

Upvotes: 0

Views: 348

Answers (2)

balkaran singh
balkaran singh

Reputation: 2786

for your anser you have to understand the Core Data stack

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

Changes that you make to your managed objects are not committed to the parent store until you save the context.

Upvotes: 1

Swinny89
Swinny89

Reputation: 7373

According to your comments on your question, you ARE calling saveContext().

Go into your AppDelegate and check out applicationWillTerminate, saveContext() is called there.

In short, if you want to persist the data then yes, you need to call saveContext()

Upvotes: 1

Related Questions