Core Data is the first choice for storing data in iOS development. When an App has launched, we need to update the Core Data schema if some attribute are changed in the next version. Fortunately, Xcode provides a way to upgrade Core Data easily, which is called Core Data Lightweight Migration. This article will introduce how to use Core Data Lightweight Migration.
In the first section, we will first create a project and add an entity called Person
. If you already know how to create a Core Data project, please read the second section directly.
In the second section, we will add an attribute to this Person entity.
The complete code can be found in .
Table of Contents
Create a New Project
Create a Xcode project called CoreDataLightweightMigrationExample. In CoreDataLightweightMigrationExample.xcdatamodeld, add an entity called Person
, and add two attributes called name
and email
, both of which are type of String
.
Upgrade Core Data Entity
Now we need to add an attribute called phone to Person
, and this Person with phone
is the second version. When users have installed the first version of the app, and then install the second version, we hope that the second version can detect that the Person has changed and automatically upgrade the first version of the Person to the second version of the Person . And, of course, it does not destroy the existing Person data.
Add a new version to CoreDataLightweightMigrationExample.xcdatamodeld, called CoreDataLightweightMigrationExample 2.xcdatamodeld. The checked one means the version currently selected by Xcode. We can select the new version in the Model Version in File Inspector.
Select CoreDataLightweightMigrationExample 2.xcdatamodeld, and add an attribute called phone
in Person.
Then, add the phone declaration in Person+CoreDataProperties.swift.
extension Person { @nonobjc public class func fetchRequest() -> NSFetchRequest<Person> { return NSFetchRequest<Person>(entityName: "Person") } @NSManaged public var name: String? @NSManaged public var email: String? @NSManaged public var phone: String? }
Next, we need to tell Core Data to automatically upgrade the entity when loading Person. This requires two variables to be set to true:
- shouldMigrateStoreAutomatically: Tell Core Data to automatically do version upgrades (Migration)
- shouldInferMappingModelAutomatically: Tell Core Data to automatically derive the mapping model.
persistentContainer = NSPersistentContainer(name: "CoreDataLightweightMigrationExam") let description = persistentContainer.persistentStoreDescriptions[0] description.shouldMigrateStoreAutomatically = true description.shouldInferMappingModelAutomatically = true persistentContainer.persistentStoreDescriptions = [description] persistentContainer.loadPersistentStores { (storeDescription, error) in if let error = error { fatalError("Error loading store \(storeDescription), \(error)") } } managedObjectContext = persistentContainer.newBackgroundContext()
Conclusion
The Lightweight Migration provided by Core Data is very powerful. As App continues to develop, new requirements often require adding new attributes or changing the type of attributes. Core Data manages these changes so that developers can focus on logical development.