views:

1461

answers:

3

I've created all of my Managed Objects after mapping out all entities/attributes/relationships in the data model. Now I have the problem of needing to add additional attributes/relationships I haven't thought of when I first designed one of my entities/classes. Is there a way to modify my existing NSManagedObject class with Core Data short of wiping all of my models and re-creating them based on the new xcdatamodel?

Will adding the attribute in the xcdatamodel update the underlying storage mechanism also? Say if I'm using SQLite3 as my persistent storage, will it add the column accordingly?

+3  A: 

If you mean "can I change my xcdatamodel and just merge changes from the generated code into my existing code for the NSManagedObject derived classes," yes, that is simple. You just generate the code for the models that have changed then merge the changes by hand into those particular derived classes. Since the changes sound like they're just additional attributes and relationships, this should be trivial - in fact, you can probably use diff and patch to do this semi-automatically if your changes are truly additive in nature.

However, if you mean you need to migrate an existing store to a new schema, you have some work ahead of you. There are certain conditions (adding orthogonal entities, fetched properties, etc) that won't force you to do this. You will realize if you need to or not when you try to add your existing persistent stores to the persistent store coordinator for your managed object context.

Before you decide to embark on doing a schema change, you should always read up on how to do migrations and versioning in Core Data - if you have existing stores you need to retain. This is almost assuredly the case in apps that have user data stored in Core Data stores. And unless you have an automated import tool or data store generation utility your existing static stores are also likely to need migration.

groundhog
A: 

You should also check out the Generation Gap design patters. It will help you in exactly this situation. Here is an SO question about using a generation gap with CoreData.

Roger Nolan
+3  A: 

As groundhog points out, for complex changes to your data model you'll need to create versions of your model and migrate data under the older model into the new one, following Apple's guide on the matter (which he links to). Don't worry about any of the behind-the-scenes SQL, Core Data handles that for you.

However, for simple data model changes, Apple has introduced a new feature in the iPhone OS 3.0 implementation of Core Data called lightweight migration. For lightwight migration, Core Data will automatically migrate across simple changes in your data model, such as changing the name of an attribute or entity, deleting an attribute, adding an attribute with a default value, or changing the inheritance of an entity. You just need to enter in the renaming identifier in the new version to point to the older version's name for something, etc. Core Data will handle the updates of your data in an efficient manner, as long as you set the NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption options on your persistent store.

Brad Larson