IOS How to make use of core data after adding some more relationship to existing core data?

耗尽温柔 提交于 2019-12-13 02:55:01

问题


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:

  1. Open Xcode project/workspace
  2. Select you Data Model file
  3. Go to Menu and click on the Edit menu. Click on the Add->Add Model version.
  4. Name you new model version, and select the previous active version as base model.
  5. Again select the .xcdatamodeld, and go to file navigator. Mark the new Model as Current version.
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!