0

Is is possible to prepend some “magic number” data to a core data persistent store?

I have an older application which uses a custom binary file format. Each file starts with a 5-byte magic number (let's say 0A 0B 0C 0D 0E) and a 1-byte format version identifier; after which follows the actual data. Existing versions of the application do the following check when a user tries to open a file:

  • If the first 5 bytes of the file don't match the magic number, the user gets a message like “The file is not in a format that this application can read. It may have the wrong file extension.”
  • If the magic number matches, but the version identifier is higher than what the version of the application can read, the user gets a message like “The file was created with a newer version of this application. You need to upgrade to the new version.”

I would like to leverage core data for storage in a new version of the application, while still using the same file extension and retaining appropriate compatibility with older versions of the application: users that try to use an old version to open a file written with the new version should get the message that they should upgrade, rather than the other message. Is this possible? And how?

Rinzwind
  • 1,173
  • 11
  • 23

1 Answers1

0

The NSManagedObjectModel has a versionIdentifiers which returns a set of developer defined strings that identify the "included" model versions. (Remember, when migrating to a new model version, the old ones are included in the model file/directory.)

Actually, the documentation states

This value is meant to be used as a debugging hint to help you determine the models that were combined to create a merged model.

but you could still use the contents of this property exactly for your purposes.

As for a "magic number", it seems to me that you are trying to invalidate a data store format without the user's consent. Bad idea. In my opinion, you should just choose another design pattern.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks for the answer, but I'm afraid I don't understand. Your answer seems to be about managing different versions of a Core Data based file format. But the question is on migrating an existing custom file format (*not* based on Core Data) to one based on Core Data. The idea would be that every file still starts with the same 5-byte magic number, but a different 1-byte version identifier. This way users get the appropriate message when trying to open the file with older versions of the application. – Rinzwind Dec 31 '12 at 12:58
  • Transforming your store to Core Data is a completely different problem. Just use your existing logic to open the store and copy it over to core data record by record. – Mundi Dec 31 '12 at 14:58