Untitled2
Untitled2
core data
persistence
xcode
ios
Natascha Fadeeva
iOS dev & creator of this site
A database migration is
performed whenever we need
to make changes to the data
model.
For a specific set of changes,
Core Data can perform an
almost automatic data
migration, also called
lightweight migration.
Examples of those changes are
adding, renaming or deleting
entities, attributes or
relationships or changing the
relationship type.
When changes to the data
model exceed the capabilities of
a lightweight migration, we
need to do a heavyweight i.e.
manual migration.
In this guide, we are going to
look at how to perform a
lightweight migration in Core
Data.
NSInferMappingModelAutomati
callyOption: true]
do {
try
coordinator.addPersistentStore(
ofType: NSSQLiteStoreType,
configurationName: nil, at: url,
options: options)
} catch {
// handle error
}
2. Creating a new data model
version
To perform automatic
lightweight migration, Core
Data generates an inferred
mapping model. To do that,
Core Data needs the new and
the old data model.
For that reason, changing a data
model that already has been
shipped to users would result in
a data base error. To avoid that,
we can create a new model
version by selecting Editor >
Add Model Version in Xcode's
menu. This will add a
new .xcdatamodel file to our
project: