CoreData 轻量数据升级

来源:互联网 发布:百度地图js api 实例 编辑:程序博客网 时间:2024/04/27 23:47

如果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中

[html] view plaincopy
  1. NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],  
  2.                                        NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES],  
  3.                                        NSInferMappingModelAutomaticallyOption, nil];  
  4.       
  5. if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType   
  6.                                                   configuration:nil   
  7.                                                             URL:storeUrl   
  8.                                                         options:optionsDictionary   
  9.                                                           error:&error]) {  
  10.   
  11.           NSLog(@"failed to add persistent store with type to persistent store coordinator");  
  12.   
  13. }  
添加 *optionsDictionary,原来options:nil  改成options:optionsDictionary


5.重新编译下程序。

0 0
原创粉丝点击