如果iOS App 使用到CoreData,并且在上一個版本上有數(shù)據(jù)庫更新(新增表口柳、字段等操作)跃闹,那在覆蓋安裝程序時就要進行CoreData數(shù)據(jù)庫的遷移,具體操作如下:
- 選中你的mydata.xcdatamodeld文件望艺,選擇菜單editor->Add Model Version 比如取名:mydata2.xcdatamodel
- 設(shè)置當前版本
選擇上級mydata.xcdatamodeld 找默,在inspector中的Versioned Core Data Model選擇Current模版為mydata2 - 修改新數(shù)據(jù)模型mydata2,在新的文件上添加字段及表
- 刪除原來的類文件店煞,重新生成下類。
在app delegate中
NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],
NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:optionsDictionary
error:&error]) {
NSLog(@"failed to add persistent store with type to persistent store coordinator");
}
添加 *optionsDictionary风钻,原來options:nil 改成options:optionsDictionary
重新編譯下程序顷蟀。