CoreData 轻量数据升级

如果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 plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值