I am trying to add a new model. The new model will have one new entity and a relationship from a current entity to the new one. I have created the new model and set it as current. Although everything works fine and i can access the new entity, some 'binary data' entries are nil.
I tried adding a mapping model and then migrating, but the results are the same. The 'binary data' stored are UIImage. Some small thumbnails are being migrated. None of the full size UIImages are being migrated.
I am thinking that maybe the entities that are automatically stored externally (due to their size) are not being migrated.
UPDATE
I build a small project that stores image collections. I tried to create a new model and everything worked fine. Then i compare the new project with the old one and the only difference was the "Allows External Storage". I selected this option on the new project, created a new model and the pictures were missing.
I am thinking that maybe the entities that are automatically stored externally (due to their size) are not being migrated.
That's exactly what happens. It's an Apple bug. Binary data over a certain size gets stored by Core Data in an external support folder within the Documents folder. For some reason, during a migration, Core Data simply wipes that external storage folder - gulp! So thumbnail image data migrates ok as it's not stored externally to the SQLite DB but larger external binary files just get deleted.
The solution I’ve come up with is when your code initializes a persistent store coordinator for your Core Data model run a few checks before attempting automatic migration. Check whether the new model is compatible with the current stored model. If it’s not then you know that Core Data is about to migrate your old model to your new version and in doing so will wipe the external storage folder. Before it does so simply move the external storage folder to a temporary location. Once the migration has completed replace new empty external storage folder generated by Core Data.
I've documented the full solution with source code in this blog post:
http://www.nickkuh.com/iphone/core-data-migration-woes-with-binary-data-and-external-storage-data-loss/2012/06/
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