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

来源:互联网 发布:广州丽晶软件 编辑:程序博客网 时间:2024/04/27 03:52

如果在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.

原创粉丝点击