您的位置:首页 > 其它

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

2013-04-20 13:58 239 查看
如果在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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: