Package-based Core Data documents

It's taken a heck of a long time, but a couple months ago, Apple finally published details on how to use NSPersistentDocument for a package-based document. This is something that we do in Sandvox 1.5 and caused much headscratching on how best to achieve it. It's good to have some kind of an official guide since most every implementation out there (that I've seen) has some subtle bug somewhere in it.

Things still aren't perfect - using a file wrapper could get pretty memory intensive - and atomic saves go out of the window, but it's a start. I guess as Apple points out in the ReadMe, the level of atomicity required is up to you. I wondered if one could get clever and represent every resource in the document package as a persistent store. i.e. create a tiny managed object model for each resource, some kind of NSAtomicStore subclass, and add them to the persistent store coordinator as required. But even then, I'm not sure how atomic save operations would be.

I guess part of the problem with any sort of package-based app (or maybe any document-based application!) is the degree of protection to offer against the user abusing the existing document on disk. How should the app behave if the document is deleted, or placed on a disk which is then removed? Should it flat out refuse to save the document again until a suitable file is returned to the original location, or should an attempt be made at guaranteeing that some base level of the document remains available?

© Mike Abdullah 2007-2015