<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSPersistentDocument",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPersistentDocument"
  },
  "title" : "NSPersistentDocument"
}
-->

# NSPersistentDocument

A document object that can integrate with Core Data.

```
class NSPersistentDocument
```

## Overview

The [`NSPersistentDocument`](/documentation/AppKit/NSPersistentDocument) class is a subclass of [`NSDocument`](/documentation/AppKit/NSDocument) that is designed to easily integrate into the Core Data framework. It provides methods to access a document-wide <doc://com.apple.documentation/documentation/CoreData/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:)`](/documentation/AppKit/NSPersistentDocument/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 <doc://com.apple.documentation/documentation/CoreData/NSManagedObjectContext/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 <doc://com.apple.documentation/documentation/CoreData/NSManagedObjectContext/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`](/documentation/AppKit/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`](/documentation/AppKit/NSPersistentDocument/managedObjectModel) property and [`configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:)`](/documentation/AppKit/NSPersistentDocument/configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:)) method. You might wish to do this, for example, to specify a particular managed object model.

> Important:
> ``doc://com.apple.appkit/documentation/AppKit/NSPersistentDocument`` does not support some document behaviors:
> 
> - File wrappers.
> - ``doc://com.apple.appkit/documentation/AppKit/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.
> 
> ``doc://com.apple.appkit/documentation/AppKit/NSPersistentDocument`` does not support the asynchronous saving API of ``doc://com.apple.appkit/documentation/AppKit/NSDocument`` because that API requires accessing the document’s state on multiple threads and that violates the requirements of the <doc://com.apple.documentation/documentation/CoreData/NSManagedObjectContext> class. Do not override ``doc://com.apple.appkit/documentation/AppKit/NSDocument/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:
> 
> - ``doc://com.apple.appkit/documentation/AppKit/NSDocument/hasUndoManager``
> - ``doc://com.apple.appkit/documentation/AppKit/NSDocument/undoManager``

The [`isDocumentEdited`](/documentation/AppKit/NSDocument/isDocumentEdited) method returns <doc://com.apple.documentation/documentation/Swift/true> if the persistent document’s managed object context, or editors registered with the context, have uncommitted changes, otherwise it returns <doc://com.apple.documentation/documentation/Swift/false>.

## Topics

### Managing the Persistence Objects

[`managedObjectContext`](/documentation/AppKit/NSPersistentDocument/managedObjectContext)

The managed object context for the document.

[`managedObjectModel`](/documentation/AppKit/NSPersistentDocument/managedObjectModel)

The managed object model of the document.

[`configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:)`](/documentation/AppKit/NSPersistentDocument/configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:))

Configures the receiver’s persistent store coordinator with the appropriate stores for a given URL.

[`persistentStoreType(forFileType:)`](/documentation/AppKit/NSPersistentDocument/persistentStoreType(forFileType:))

Returns the type of persistent store associated with the specified file type.

### Document Content Management

[`read(from:ofType:)`](/documentation/AppKit/NSPersistentDocument/read(from:ofType:))

Sets the contents of the receiver by reading from a file of a given type located by a given URL.

[`revert(toContentsOf:ofType:)`](/documentation/AppKit/NSPersistentDocument/revert(toContentsOf:ofType:))

Overridden to clean up the managed object context and controllers during a revert.

[`write(to:ofType:for:originalContentsURL:)`](/documentation/AppKit/NSPersistentDocument/write(to:ofType:for:originalContentsURL:))

Saves changes in the document’s managed object context and saves the document’s persistent store to a given URL.

### Deprecated

[`configurePersistentStoreCoordinatorForURL:ofType:error:`](/documentation/AppKit/NSPersistentDocument/configurePersistentStoreCoordinatorForURL:ofType:error:)

Configures the receiver’s persistent store coordinator for a given URL and document type.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)