Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Core Data Migration leads to binary data loss when allow external storage selected

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.

like image 416
zirinisp Avatar asked Apr 25 '12 09:04

zirinisp


1 Answers

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/

like image 82
Nick Kuh Avatar answered Sep 30 '22 22:09

Nick Kuh