Search code examples
xcodecore-datacore-data-migration

Core Data change property from Integer 16 to Integer 32


I'm having a very serious problem. The application is live, but unfortunately it's fails on iOS 5, and I need to post an update.

The thing is the ID column of few entities is in Integer 16, but I need to be changed to Integer 32.

It was clearly my mistake, the model was created very long time ago, and it was only being reused. To my surprise (now) on iOS 4, Integer 16 in Core Data could easily keep number as big as 500 000 (bug?), but it doesn't work like that now - it gives me invalid numbers.

Application is live, has it success and Core Data is also used to keep the users scores, achievements and so on, what I don't want to remove, forcing them to reinstall the application. What is the best approach to simply change about ten of properties in different entities from Integer 16 to Integer 32?

Of course I know the names and entities for those properties.

If I just change the Type column for those properties in the xcdatamodeld file it will work, for new user, but what about existing users, that already have sqlite file in their Documents folder. I believe I need to change the persistent store coordinator somehow.

And also what do you thing about the performance, there are about 10 properties that news to be changed from 16 to 32, but Core Data have in usual cases more than 100 000 objects inside.

Regards


Solution

  • Turn on NSMigratePersistentStoresAutomaticallyOption' and 'NSInferMappingModelAutomaticallyOption in your NSPersistentStore and then create a second version of your model with the changes. Only do the integer changes to keep the migration simple. That will allow users who install your upgrade to migrate from the broken model to the corrected model.

    NOTE: This must be an automatic migration; a manual migration with a mapping model will not work.