如果iOS App 使用到CoreData,并且在上一個版本上有數據庫更新(新增表、字段等操作),那在覆蓋安裝程序時就要進行CoreData數據庫的遷移,具體操作如下:
1.選中你的mydata.xcdatamodeld文件,選擇菜單editor->Add Model Version? 比如取名:mydata2.xcdatamodel
2.設置當前版本
?? 選擇上級mydata.xcdatamodeld ,在inspector中的Versioned Core Data Model選擇Current模版為mydata2
3.修改新數據模型mydata2,在新的文件上添加字段及表
4.刪除原來的類文件,重新生成下類。(這一步沒有做)
在appdelegate中
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
5.重新編譯下程序