Changing Attribute Type in Core Data with NSInferMappingModelAutomaticallyOption

Wise Shepherd picture Wise Shepherd · Jul 8, 2013 · Viewed 7.8k times · Source

I have my Core Data Store and I use the flag 'NSInferMappingModelAutomaticallyOption' so that whenever I make changes to the Core Data Model I first create a new model version, and the changes to the data models are automatically migrated.

However, I need to change an attribute type for one of my Entities. When I do this, the automatic migration doesn't seem to work and I get a Core Data error when I try to run my app.

Any way of setting this up to continue with the automatic model migration?

Answer

eofster picture eofster · Jul 8, 2013

Attribute type change is not supported by the lightweight migration:

For Core Data to be able to generate an inferred mapping model, changes must fit an obvious migration pattern, for example:

  • Simple addition of a new attribute
  • Removal of an attribute
  • A non-optional attribute becoming optional
  • An optional attribute becoming non-optional, and defining a default value
  • Renaming an entity or property

Edit

I assume you're using the lightweight migration. But as Scott pointed out, maybe you aren't. If you want to use automatic migration (which is not the same as the lightweight migration), you can still do this by providing the mapping model. In this case:

Core Data will attempt to locate the source and mapping models in the application bundles, and perform a migration.

But this migration will not be lightweight. Core Data will open two stores–the source and the target–and copy all entities in memory. So the memory consumed by the app depends on the amount of data in the store.