coredata 轻量级 迁移(4.2以下)
coredata轻量级 迁移(4.2以上)当CoreData中的Entity,Property或者Relationship发生改变以后,默认情况下面,
coredata 轻量级 迁移(4.2以上)
当CoreData中的Entity,Property或者Relationship发生改变以后,默认情况下面,在尝试调用CoreData的时候,程序会异常退出。
?
?
怎样开启自动迁移的功能?分三步
1.在App?delegate类里修改persistentStoreCoordinator方法
?
?
- -?(NSPersistentStoreCoordinator?*)persistentStoreCoordinator?{??
- ????if?(__persistentStoreCoordinator?!=?nil)?{??
- ????????return?__persistentStoreCoordinator;??
- ????}??
- ??????
- ????NSURL?*storeURL?=?[[self?applicationDocumentsDirectory]?URLByAppendingPathComponent:@"WorkXP.sqlite"];??
- ??????
- ????NSError?*error?=?nil;??
- ????__persistentStoreCoordinator?=?[[NSPersistentStoreCoordinator?alloc]?initWithManagedObjectModel:[self?managedObjectModel]];??
- ??????
- ????//?handle?db?upgrade??
- ????NSDictionary?*options?=?[NSDictionary?dictionaryWithObjectsAndKeys:??
- ?????????????????????????????[NSNumber?numberWithBool:YES],?NSMigratePersistentStoresAutomaticallyOption,??
- ?????????????????????????????[NSNumber?numberWithBool:YES],?NSInferMappingModelAutomaticallyOption,?nil];??
- ??????
- ????if?(![__persistentStoreCoordinator?addPersistentStoreWithType:NSSQLiteStoreType?configuration:nil?URL:storeURL?options:options?error:&error])?{??
- ???????//?Handle?error??
- ????}??????
- ??????
- ????return?__persistentStoreCoordinator;??
- } ?
?
?
2. 创建新版本并选择当前active模型版本,
xcode 4里,创建新模型是在Editor里的Add Model Verison,
?
要选择当前使用的数据模型版本,在文件导航中,先选中appname.xcdatamodeld这个,然后在右侧属性栏中Versiond Core Data Model中选择Current的模型版本。
?

3.OK了,再次运行,App可以访问新版本的coredata数据库模型了。