Nicolò Ciraci
Nicolò Ciraci

Reputation: 678

Coredata migration: change value type

I've an already shipped application that use Coredata so save all the data. My model defines a value of type BinaryData and I would like to change the type to Integer.

Currently that field is unused, but his type is incorrect. Can I migrate my store without pain? I've tried some approaches but none of them actually worked.

Any ideas?

Upvotes: 1

Views: 607

Answers (1)

Mundi
Mundi

Reputation: 80265

The proper way would be to use a mapping model, but I think there is a much more practical solution for you. Because the field was never used, just delete it. The overhead is practically inexistent.

Now all you have to do is lightweight migration:

  • Create a new model version.
  • Add the Int attribute, delete the old one.
  • Change the active model version to the new one.
  • Change the options in your call to addPersistentStore to include
    • NSMigratePersistentStoresAutomaticallyOption
    • NSInferMappingModelAutomaticallyOption
  • Change your code to use the new attribute
  • Eliminate all potential uses of the old attribute from your code

Test it thoroughly before you upload ;-).

Upvotes: 2

Related Questions