---
title: NSPersistentDocument
framework: appkit
role: symbol
role_heading: Class
path: appkit/nspersistentdocument
---

# NSPersistentDocument

A document object that can integrate with Core Data.

## Declaration

```swift
class NSPersistentDocument
```

## Overview

Overview The NSPersistentDocument class is a subclass of NSDocument that is designed to easily integrate into the Core Data framework. It provides methods to access a document-wide NSManagedObjectContext object, and provides default implementations of methods to read and write files using the persistence framework. In a persistent document, the undo manager functionality is taken over by managed object context. Standard document behavior is implemented as follows: Opening a document invokes configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:) with the new URL, and adds a store of the default type (XML). Objects are loaded from the persistent store on demand through the document’s context. Saving a new document adds a store of the default type with the chosen URL and invokes save: on the context. For an existing document, a save just invokes save() on the context. Save As for a new document simply invokes save. For an opened document, it migrates the persistent store to the new URL and invokes save() on the context. Revert resets the document’s managed object context. Objects are subsequently loaded from the persistent store on demand, as with opening a new document. By default an NSPersistentDocument instance creates its own ready-to-use persistence stack including managed object context, persistent object store coordinator and persistent store. There is a one-to-one mapping between the document and the backing object store. You can customize the architecture of the persistence stack by overriding the managedObjectModel property and configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:) method. You might wish to do this, for example, to specify a particular managed object model. important: NSPersistentDocument does not support some document behaviors: File wrappers. NSDocument.SaveOperationType.saveToOperation operation type. Core Data does not support saving changes to a new document while maintaining the unsaved state in the current document. Asynchronous saving. NSPersistentDocument does not support the asynchronous saving API of NSDocument because that API requires accessing the document’s state on multiple threads and that violates the requirements of the NSManagedObjectContext class. Do not override canAsynchronouslyWrite(to:ofType:for:). Undo Support The persistent document uses the managed object context’s undo manager. important: Do not override the following properties, their getters, or their setters: hasUndoManager undoManager The isDocumentEdited method returns true if the persistent document’s managed object context, or editors registered with the context, have uncommitted changes, otherwise it returns false.

## Topics

### Managing the Persistence Objects

- [managedObjectContext](appkit/nspersistentdocument/managedobjectcontext.md)
- [managedObjectModel](appkit/nspersistentdocument/managedobjectmodel.md)
- [configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:)](appkit/nspersistentdocument/configurepersistentstorecoordinator(for:oftype:modelconfiguration:storeoptions:).md)
- [persistentStoreType(forFileType:)](appkit/nspersistentdocument/persistentstoretype(forfiletype:).md)

### Document Content Management

- [read(from:ofType:)](appkit/nspersistentdocument/read(from:oftype:).md)
- [revert(toContentsOf:ofType:)](appkit/nspersistentdocument/revert(tocontentsof:oftype:).md)
- [write(to:ofType:for:originalContentsURL:)](appkit/nspersistentdocument/write(to:oftype:for:originalcontentsurl:).md)

## Relationships

### Inherits From

- [NSDocument](appkit/nsdocument.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSEditorRegistration](appkit/nseditorregistration.md)
- [NSFilePresenter](foundation/nsfilepresenter.md)
- [NSMenuItemValidation](appkit/nsmenuitemvalidation.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSUserActivityRestoring](appkit/nsuseractivityrestoring.md)
- [NSUserInterfaceValidations](appkit/nsuserinterfacevalidations.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Documents

- [Developing a Document-Based App](appkit/developing-a-document-based-app.md)
- [NSDocument](appkit/nsdocument.md)
- [NSDocumentController](appkit/nsdocumentcontroller.md)
