Migration can be done without multiple data model versions. Hurrah?

喜欢而已 提交于 2020-03-03 09:07:16

问题


I was referring a wonderful tutorial Swift Core Data Tutorial for migrations in core data.

The tut stated that migrations in core data go hand in hand with multiple versions of data model.

I was experimenting on light-weight migrations and got to figure out the following points:

Scenario - I had a data model and I tried to add a some new attributes to an entity and I got an error as -

iOS Version - 9.1 Swift Version - 2.2

"The model used to open the store is incompatible with the one used to create the store”

I then tried to enable migration without creating any new version by adding the code -

let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]

try persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: URLPersistentStore, options: options)

Then I tried altering the database similarly, i.e added an attribute to an entity and hurrah !!, it didn't result in a crash. Even I tried to access the particular entity as well as attribute but did it successfully.

What happened is migration without having multiple versions.

Question - So Is migration possible without having multiple versions of data model?

Link to sample code - https://www.dropbox.com/s/mnb2dzxn56ghuet/CoreDataSwift-NSFetchedResultsController-master-2.zip?dl=0


回答1:


Beginning with iOS 9, Core Data will copy the data model to the persistent store, and use that as the starting point if lightweight migration is necessary and requested. As a result, lightweight migration is possible even if the old model version is not available. This only works with SQLite persistent stores, and only for lightweight model migrations.

This was described in the What's New in Core Data session at WWDC 2015.



来源:https://stackoverflow.com/questions/39207492/migration-can-be-done-without-multiple-data-model-versions-hurrah

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