A managed document object that integrates with Core Data.


class UIManagedDocument : UIDocument


UIManagedDocument is a concrete subclass of UIDocument. When you initialize a managed document, you specify the URL for the document location. The document object then creates a Core Data stack to use to access the document’s persistent store using a managed object model from the app’s main bundle. UIManagedDocument performs all the basic setup you need for Core Data, and in some cases you may use instances of the class directly (without a need to subclass). You can supply configuration options for the creation of the coordinator using persistentStoreOptions, and for the model using modelConfiguration. You can also perform additional customization by creating a subclass of UIManagedDocument:

Handling Errors

To enable your app to observe and handle errors in saving and validating a managed document, you must subclass the UIManagedDocument class and override one or both of the following two inherited methods from the UIDocument class:

Overriding is required because otherwise, the only information your app receives on error is the stateChangedNotification notification, which does not contain a userInfo dictionary and so does not convey specific error information.


Managing the Core Data Stack

var managedObjectContext: NSManagedObjectContext

The document’s managed object context.

var managedObjectModel: NSManagedObjectModel

The document’s managed object model.

var persistentStoreOptions: [AnyHashable : Any]?

Options used when creating the document’s persistent store.

var modelConfiguration: String?

A model configuration name to be passed when configuring the persistent store.

func persistentStoreType(forFileType: String) -> String

Returns the Core Data store type for a given document file type.

Customizing Read and Write Operations

func readAdditionalContent(from: URL)

Handles reading non-Core Data content in the additional content directory in the document’s file package.

func additionalContent(for: URL) -> Any

Handles writing non-Core Data content to the additional content directory in the document’s file package.

func writeAdditionalContent(Any, to: URL, originalContentsURL: URL?)

Handles writing non-Core Data content to the document’s file package.

Naming the Persistent Store File

class var persistentStoreName: String

Returns the name for the persistent store file inside the document’s file package.

See Also


class UIDocument

An abstract base class for managing discrete portions of your app's data.