问题
I've decided to change my core data structure by adding few relationship (2-3) relationship. How to switch to the new core data stack but still keeping the old implementation (method, data....). Do we need to create new xcdatamodel
and how switch to new xcdatamodel?
Any help is much appreciate
回答1:
When you change your data model, you need to inform CodeData of the change as well. For this a'lightWeightMigration' is needed. To tell Core Data we want to perform a lightweight migration, we need to set two properties when configuring our persistent store coordinator in the startCoreData() method. First, NSMigratePersistentStoresAutomaticallyOption needs to be true, which tells Core Data to upgrade its SQLite database when the model changes. Second, NSInferMappingModelAutomaticallyOption also needs to be true, which tells Core Data to figure out the differences when the model changes, and apply sensible defaults if possible create the optionsDictionary as this : let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] pass this optionsDictionary in coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions) And you are good to go!
回答2:
If you're working with the existing app which is already on the Apple Store, It is very important to make a new Data Model
To Create new Data Model, Go thorough the following steps:
- Open Xcode project/workspace
- Select you Data Model file
- Go to Menu and click on the Edit menu. Click on the Add->Add Model version.
- Name you new model version, and select the previous active version as base model.
- Again select the
.xcdatamodeld
, and go to file navigator. Mark the new Model as Current version. - Start working on the new database model.
Hope this will help you :)
来源:https://stackoverflow.com/questions/38784884/ios-how-to-make-use-of-core-data-after-adding-some-more-relationship-to-existing