Reputation: 1255
I have to add one entity in my coreData project but I can't understand how to do the lightweight migrations in swift. I'm stuck at point 8, how can i traslate that code from objective-c to swift? Thank you!
1-Open your .xcdatamodeld file
2-click on Editor
3-select Add model version...
4-Add a new version of your model (the new group of datamodels added)
5-select the main file, open file inspector (right-hand panel)
6-and under Versioned core data model select your new version of data model for current data model 7-THAT'S NOT ALL ) You should perform so called "light migration".
8-Go to your AppDelegate and find where the persistentStoreCoordinator is being created
9-Find this line if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
10-Replace nil options with @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} (actually provided in the commented code in that method)
11-Here you go, have fun!
Upvotes: 2
Views: 734
Reputation: 2333
In a template project the line that create the persistent store is
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
To perform a lightweight migration you have to pass the following dictionary as options argument, instead of nil
:
[NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
Upvotes: 8