Archive for 4th January 2012

Renaming a Core Data attribute when creating a new model version

I found this one out the hard way.

If you are doing simple attribute additions when you transition from one Core Data model version to another, you can just create a new model version, set your overall data model’s default to the new version, and add the needed attributes to the entities as you need. When you fire up the app, the new fields are magically added to the table without any additional work.

However, when you want to rename an attribute, if you follow the above series of steps, the data that exists in the fields corresponding to the old attribute name is now replaced with a shiny new nil. Outstanding!

I guess I didn’t read the Apple Core Data docs well enough. Make sure that, when you add your model version and rename the attribute, make sure that you can see the Utilities view on the right side of the Xcode 4 window, click on the attribute you are renaming, click on the Data Model inspector (the third icon in the Utilities view, looks like a little database cylinder with an eye patch on it), and for the Renaming ID entry, type in the old name of the attribute. Once you do this, the data in the field should migrate forward.

Luckily, I didn’t blow away too many beta user’s data discovering this one.

BTW, a posthumous Happy Birthday to James Bond, the ornithologist whose book Ian Fleming had on his bookshelf and reportedly named his super spy character after.