Saving Settings with Core Data Metadata

I’m working on a Core Data app where I needed to save some document settings. Creating Core Data entities for these settings was overkill. In my research for a solution I discovered persistent store metadata. I did not find much information online on using persistent store metadata so I’m sharing what I learned in this article.

This article is not for people new to Core Data. If you’re new to Core Data, start by reading Core Data Programming Guide, which is part of Apple’s developer documentation.

Persistent Store Metadata

Core Data uses persistent stores to save data. While you can use an in-memory store, most persistent stores are files. In a document-based Core Data app the document file contains the persistent store.

Persistent stores have a metadata property to store metadata. The metadata is a dictionary. The dictionary keys are strings, and the dictionary values can be any data type. Core Data stores the store type and unique identifier in the metadata. You can also add your own entries to the metadata to store small pieces of data that don’t belong in a Core Data entity.

The most common reason for someone to use persistent store metadata is to save per-document settings in Core Data apps that use NSPersistentDocument. Most people who use Core Data have no need to deal with the metadata. They can stick with Core Data entities and Apple’s UserDefaults class. But there are edge cases where saving data in the persistent store’s metadata makes sense.

Getting the Persistent Store

The persistent store contains the metadata. If you want to access the metadata, you must access the persistent store. The managed object context has a persistent store coordinator. The coordinator contains an array of persistent stores. The store is the first element in the array.

Keep in mind the array of persistent stores can be empty. If you create a new document in a document-based Core Data application, a persistent store is not created until you save the document. Check that the persistentStores array is not empty before accessing its elements.

Updating the Metadata

Once you have access to the persistent store, take the following steps to fill the metadata:

Create a variable for the metadata.

Set the variable to the store’s metadata property.

Add dictionary entries for your fields.

Set the store’s metadata property to your variable.

Core Data saves some internal data in the metadata. You should be adding your fields to the existing metadata, not overwriting the existing metadata with your fields. That’s why you create a variable for the metadata and set its initial value to the store’s existing metadata before you add any fields to the metadata.

The following Swift code demonstrates how to update the metadata to store the author of a document:

After updating the metadata, save the managed object context to save the metadata. If you stick with simpler data types like strings and integers, you can save without having to write any more code. If you try to save something more complicated, such as an enum, you will have to write code to conform to the Codable protocol or your app will crash when you save.

NSPersistentStoreCoordinator also has class and instance methods, both named setMetadata, to set the metadata. I went with the simplest solution, setting the store’s metadata property.

When to Update the Metadata?

A good time to update the metadata is when you’re about to save the managed object context. Register for the notification NSManagedObjectContextWillSave.

Retrieving the Metadata

Call the NSPersistentStoreCoordinator instance method metadataForPersistentStore to retrieve the metadata for your persistent store. Supply the persistent store as the argument to metadataForPersistentStore.

The following Swift code demonstrates how to retrieve the document’s author from the metadata: