Sunday, April 18, 2010

Add Custom Managed Object Class

I use Core Data in my first iPhone application. During the development, I have many occasions to change/update my entity objects in my managed data model. For each change, I have to update the custom managed object class. XCode provides a template to create custom managed object class I generated before. However, it is very tricky to use this template. The key point is that you have select an Entity in Data Model first. Here are the steps to that.

Managed object classes are by default located in Resource group. The first step is to remove the old managed object class. For example, I added a new property "note" to entity "paymentType". Therefore, the first step is to remove the paymentType.m and .h files from Resource group.

Next, select the entity in .xcdatamodel, "paymentType". Then from the menu File, select "New File...".

Choose the template Managed Object Class in Cocoa Class:

All the next steps are very straightforward. Continue next to finish.

Apple's developer's web page provides similar information about this issue.

Another very critical thing to do is to remove the application in both device (iTouch or iPhone) and simulator. Since the managed data model has been changed, the updated .sqlite database most likely may not be compatible with the previous version. Therefore, the sqlite file has to be removed.