关于Coredata版本升级的文章很多,这里提一下自己碰到的可能会出错的地方。
1、必须修改AppDelegate.m下的 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator内方法。让其支持coredata数据结构升级,否则只能通过删除app再编译来升级。。
意义为:自动迁移数据库
ios开发之使用CoreData的轻量级自动数据迁
方法如下:
iOS App升级安装 - CoreData数据库升级
使用coredata导致的版本更新后程序crash的问题
iOS/Xcode异常:reason = “The model used to open the store is incompatible with the one used to create the store”
简单地说就是在
- (
NSPersistentStoreCoordinator
*)persistentStoreCoordinator 里添加:
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])
的options从nil改为options。
即可切换版本号.
注意:NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"yyg 2.sqlite"];
里地
@"yyg 2.sqlite"根据当前的coredata替换,