1
votes

I have an OS X app that relies on NSPersistentDocument and Core Data to handle the app's custom file type (which is being saved as XML).

I've noticed that when I update my Core Data model (I'm making simple changes, relying on lightweight migration) simply opening a file with the app that's utilizing the new model will change the file on disk. A backup version of the original file also appears on disk (with a '~' appended to the file name). Once this happens, it's impossible for the previous (released) version of the app to open the file.

To be clear: I'm simply opening the file. I am not saving the file back to disk. I can watch in the Finder and see the '~' appear as soon as I open a file.

This seems like really dangerous behavior. I have to be very cautious and not ever open a file that's currently in use by my user base because doing so will instantly make it inaccessible to them.

Is this default OS X behavior? Is there a way to configure NSPersistentDocument or my Core Data model so that opening a file changes the store internally to the app but the changes don't get propagated back to disk unless I explicitly save?

1

1 Answers

0
votes

Core Data seems to assume that if you're opening an old file with a new program, you must want to go ahead with migration. From my own experience, it is default behaviour that when the file is opened, it is automatically migrated and can no longer be opened with the previous version program. I believe it has to do with the hash numbers, which must be changed to allow the new store to open the file, making it a permanent change in the document; so no, I don't think you can open the file, but not propagate the changes back to disk. Perhaps you could override a method that would allow you to make a copy before starting migration.

Apple's Developer Library mentions the temp files that are made:

If the migration proceeds successfully, the existing store at storeURL is renamed with a “~” suffix before any file extension and the migrated store saved to storeURL.

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmInitiating.html#//apple_ref/doc/uid/TP40004399-CH7-SW1