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?
You cannot use lightweight migration for this so it is a little bit harder (thanks Apple), but not impossible
step by step in Xcode 7.1:
1. Create the new model version:
.xcdatamodeld
model -> Editor
-> Add model version ...
attribute type
2. Custom Core Data mapping model
New file -> Mapping Model
(Core Data -> Mapping Model)source
(from model) and target
(to model) version of your modelNameToName
. Change value expression of the changed attribute in target entity in this file reflect the one you need to: FUNCTION($entityPolicy, "<*transformingMethodName*>" , $source.<*attributeName*>)
- transformingMethodName: Your custom method that will be called to transform attribute type. (Will define it in the next step - hold on)
- attributeName: your changed attribute name
<*EntityName*>TransformationPolicy
class as a subclass of NSEntityMigrationPolicy
transformingMethodName
you defined above. (Do what you need there to change the attribute type). Make sure you added this method to your header file as well
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With