A container that encapsulates the Core Data stack in your app.


@interface NSPersistentContainer : NSObject


NSPersistentContainer simplifies the creation and management of the Core Data stack by handling the creation of the managed object model (NSManagedObjectModel), persistent store coordinator (NSPersistentStoreCoordinator), and the managed object context (NSManagedObjectContext).


Creating a Container

- initWithName:

Initializes a persistent container with the given name.

- initWithName:managedObjectModel:

Initializes a persistent container with the given name and model.

+ persistentContainerWithName:

Initializes a new persistent container using the provided name for the container.

+ persistentContainerWithName:managedObjectModel:

Initializes a new persistent container using the provided name and managed object model.

Configuring a Container


The persistent store descriptions used to create the persistent stores referenced by this persistent container.

- loadPersistentStoresWithCompletionHandler:

Instructs the persistent container to load the persistent stores.


The model associated with this persistent container. (read-only)


The name of this persistent container. (read-only)


The persistent store coordinator associated with this persistent container. (read-only)

Accessing the Context(s)

- newBackgroundContext

Creates a private managed object context.

- performBackgroundTask:

Causes the persistent container to execute the block against a new private queue context.


The managed object context associated with the main queue. (read-only)

Configuring the Store Location

+ defaultDirectoryURL

Creates the default directory for the persistent stores on the current platform.


Inherits From

See Also

Stack Fundamentals


A programmatic representation of the .xcdatamodeld file describing your objects.


An object space that you use to manipulate and track changes to managed objects.


A coordinator that uses the model to help contexts and persistent stores communicate.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software