This post is short, concise guide to perform a Core Data light weight migration, to add an attribute to a Core Data model.
- Open the existing CoreData model object, e.g.
Model.xcdatamodeld, go to Editor menu and select Add Model Version. Call the new version something v2 and select the original model as “Based on Model” field.
- In the File Inspector on right, at the bottom in Model Version, select the v2 that was just created. In Project Navigator, the model object will show both model files and the v2 one will now have a green tick.
- Add a new attribute in the v2 model.
- Open the existing model object swift file and add a property for the new attribute.
addPersistentStoreWithType()call of NSPersistentStoreCoordinator object, supply options attribute
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true].
- Run app again and there should be no error!
- Apple Documentation Archive - Lightweight Migration