Adding an Attribute to a Core Data Entity in Xcode 4

July 31, 2011

I wanted to add an attribute to one of the objects I manage using Core Data in my iPhone app. Because it’s a simple change, I wanted to use what Apple calls Lightweight Migration–automatically migrating the data when you make a simple change like adding a field.

I had to assemble the steps from a few different places to get it to work, partially because blog posts I found were for Xcode 3, and not even all the Apple docs have been updated for Xcode 4. Here’s the full set of steps that worked for me. For example purposes, I’m assuming your app is called YourApp, and the Core Data model is called YourData.

  1. Don’t make changes to the data model yet!
  2. Run your app in the simulator to make sure it has data set up under the current version of the entity.
  3. Select the YourData.xcdatamodel file and choose Editor > Add Model Version… The default numbering scheme (“YourData 2″) is probably fine.
  4. Select the new YourData 2.xcdatamodel file, select the entity, and add the new attribute. Make sure the right pane is visible (third button above “View” on the toolbar) and the third option within that pane is selected. Either set the attribute to be optional, or set it to have a default value–you have to choose one or the other for Lightweight Migration to work.
  5. Regenerate your model classes by selecting the entity, choosing File > New > New File…, then NSManagedObject subclass. Note: I first created my classes under Xcode 3, and at that time it put the .h and .m files inside of the actual YourData.xcdatamodel file (which is really a directory on the filesystem). Xcode 4 didn’t seem to give me this option, but that’s fine–to me, it makes more sense to store them with the other classes anyway. If you do put your new .h and .m files in a new location, be sure to delete the old ones.
  6. Select the YourData.xcdatamodeld file, go to the right panel, first option, and look for Versioned Data Model > Current Version. Set this to the newer version.
  7. In YourAppAppDelegate.m, find the persistentStoreCoordinator method, or wherever you call addPersistentStoreWithType:… You will probably be passing it nil for options:. Instead, pass this:


    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

  8. Update your app to use the new attribute/property in your entity. You might want to make only small changes at first, in case something goes wrong with your auto migration.
  9. Run your app and test it.

I wrote these steps out after I finished, so please leave a comment to let me know if I missed something and you run into other trouble.


Setting Up a CentOS VM in VirtualBox

July 28, 2011

I’ve been trying to set up a CentOS VM in VirtualBox, and had a lot more trouble than I’ve had with other Linux distros. The prebuilt image I tried from virtualboxes.org had issues with the networking adapter and keyboard layout. Trying the minimal or network installer had the same problems.

I was eventually able to solve it by installing from the Live CD disk image.


Follow

Get every new post delivered to your Inbox.

Join 525 other followers