External Storage with Core DataNovember 11, 2012 -
New since iOS 5.0, we have a new weapon at our disposal for Core Data. External Storage lets you tell Core Data that you’re going to jam large data blocks (mp3s, pngs, whatever) into a property on your Managed Object. Normally you might say “Oh no! Why would you do that? Everyone knows you shouldn’t give large data blobs to Core Data! You should write them out as a separate file you idiot!” iOS 5.0 changed everything, it’s ok. Now you can let Core Data handle those large files and stop bothering to write all kinds of files and handling deleting them yourselves. Yay less code!
If you check the Google or Apple’s docs, you’ll see articles explaining how to get things running, but what you won’t find is an explanation for how Core Data deletes those files. I setup a test application to find out for myself. You can find it here. My tests show that the external files get deleted when you simply nil out the property and save your context. It will also delete the file if you delete the whole entity. Since all you need to do is nil out the property, you don’t need to make separate entities for your data blob properties. Whooo!