Quantm Quantm - 2 months ago 6
Swift Question

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 !!!

Answer

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()

Comments