Handle Core Data Model Changes

In last night’s Boston CocoaHeads meeting, we had an interesting discussion of the pros and cons of Apple’s Core Data persistence framework. It was interesting to hear about how people are using this great technology.

One of the biggest concerns that came up with using Core Data is how to handle changes to your Managed Object Model or database schema. As of Mac OS X 10.4 (Tiger), Core Data does not directly migrate your data between different versions of your model. It’s also very unforgiving of trying to open a version of your persistent store that you are not expecting.

So what happens when you need to make a change to your Core Data model? What if you need to add an entity or change an entity relationship? This is inevitable over the life of your application. Requirements change and your users needs evolve over time.

As beta testing was finishing up for the first release of LicenseKeeper, I received some great advice:

“Make sure you handle Core Data model changes before you need to.”

When that emergency change inevitably rears its ugly head, be ready. Well, the advice was golden. The emergency did show up right before the 1.0 release and I was ready.

How do you migrate Core Data libraries through model changes?

Well, this same person also wrote a great article with plenty of example code to take you through the daunting task of setting up your mostly generic Core Data migration utility:

Core Data migration between contexts

Update: I had someone ask for the identity of the article’s author. Since the article had no attribution, I was trying to respect his privacy. But, Marcus Zarra of Zarra Studios was more than happy to accept credit. Marcus also mentioned that he had made the code available for download from Google Code: