Search code examples
cocoacore-dataconceptual

What is the best way to store document specific settings in a Core Data Document?


I'm using a NSPersistantDocument backed by Core Data in my application.

Each document has a number of settings that are only relevant to that document. For example the document needs to hold the unique ID of an Address Book Group, to update it at a later date.

The way I'm thinking of approaching this is to have another model, DocumentSetting, which has two attributes; a key and a value, which hold NSString's.

Ideally I would like something similar to NSUserDefaults but stored within the document.

Has anyone done this before? Are there any better way to approach this?


Solution

  • The setMetadata: method on NSPersistentStore can be used to store per-document settings. The docs for how to go about using the store's metadata can be found here. As advised in the docs though, there are some size limitations on what can be put in there. If you need to store something more substantial than a few key/value pairs, I would recommend making a new entity in your data model to store such data.