Core Data版本更新进行Migration时renaming identifier设置的注意事项

如果在iOS开发中使用了Core Data,当需要对Model进行更新时(如修改属性名称),需要增加Model版本,并使用在NSPersistentStoreCoordinator创建时,使用相应的Migration Option,具体方法可以参考《How to Perform a Lightweight Core Data Migration》一文,本文只想提示其中一个需要注意的地方。


假设项目名为Test,则Model名为Test.xcdatamodel,即为原始版本,增加的版本则为Test 2.xcdatamodel,以此类推。


在为修改的的属性名称设置相应的“renaming identifier”时,如果原始版本中属性名为timeStamp,在Test 2.xcdatamodel中修改为when,则需要在Test 2.xcdatamodel中将renaming identifier”设置为timeStamp。


如果在第三个版本Test 3.xcdatamodel中又将when修改成了time,则这时候的renaming identifier”应该如何设置呢?答案是不用设置,因为在生成Test 3.xcdatamodel时,这个renaming identifier”已经被自动复制过来,也就是timeStamp,亦即:

这个renaming identifier”必须与原始版本中的属性名一致,而不是上一个版本一致。


在苹果的官方文档中有以下一段文字说明,请参考:

The renaming identifier creates a “canonical name,” so you should set the renaming identifier to the name of the property in the source model (unless that property already has a renaming identifier). This means you can rename a property in version 2 of a model then rename it again version 3, and the renaming will work correctly going from version 2 to version 3 or from version 1 to version 3.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值