一、 Lightweight Migration
i. 适合场景
Simple addition of a new attribute
Removal of an attribute
A non-optional attribute becoming optional
An optional attribute becoming non-optional, and defining a default value
Renaming an entity or property
ii. 如何做
1. 设置Persistent Store:
NSDictionary *persistentStoreOptions = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES };
persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:persistentStoreOptions error:&addStoreError];
2. 给database.xcdatamodel添加版本
select database.xcdatamodel -> Editor -> Add Model Version,然后设置当前数据库对应的data model文件是哪个便好。
3.这样便完成了。so easy, so simple。而此种迁移方式,占据大部分的数据库数据迁移的应用场景。
二、Multiple Passes
这项技术,我当前还未遇到需要使用它的场景,所以没有花时间研究。至于如何处理,可以参照一篇很不错的博客, http://www.cnblogs.com/liyufeng2013/p/3782351.html
Apple official doc: https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmMappingOverview.html#//apple_ref/doc/uid/TP40004399-CH5-SW1