<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreData",
  "identifier" : "/documentation/CoreData/NSPersistentContainer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Data"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPersistentContainer"
  },
  "title" : "NSPersistentContainer"
}
-->

# NSPersistentContainer

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

```
class NSPersistentContainer
```

## Overview

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

## Topics

### Creating a Container

[`-  initWithName:`](/documentation/CoreData/NSPersistentContainer/init(name:))

Creates a container with the specified name.

[`-  initWithName:managedObjectModel:`](/documentation/CoreData/NSPersistentContainer/init(name:managedObjectModel:))

Create a container with the specified name and managed object model.

[`+  persistentContainerWithName:`](/documentation/CoreData/NSPersistentContainer/persistentContainerWithName:)

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

[`+  persistentContainerWithName:managedObjectModel:`](/documentation/CoreData/NSPersistentContainer/persistentContainerWithName:managedObjectModel:)

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

### Getting the Container’s Configuration

[`managedObjectModel`](/documentation/CoreData/NSPersistentContainer/managedObjectModel)

The container’s managed object model.

[`name`](/documentation/CoreData/NSPersistentContainer/name)

The container’s name.

[`persistentStoreCoordinator`](/documentation/CoreData/NSPersistentContainer/persistentStoreCoordinator)

The container’s persistent store coordinator.

### Accessing the Default Directory

[`defaultDirectoryURL`](/documentation/CoreData/NSPersistentContainer/defaultDirectoryURL-swift.type.property)

The location of the directory that contains the persistent stores.

[`+  defaultDirectoryURL`](/documentation/CoreData/NSPersistentContainer/defaultDirectoryURL())

Returns the location of the directory that contains the persistent stores.

### Managing Persistent Stores

[`persistentStoreDescriptions`](/documentation/CoreData/NSPersistentContainer/persistentStoreDescriptions)

The descriptions of the container’s persistent stores.

[`-  loadPersistentStoresWithCompletionHandler:`](/documentation/CoreData/NSPersistentContainer/loadPersistentStores(completionHandler:))

Loads the persistent stores.

### Acquiring Contexts

[`-  newBackgroundContext`](/documentation/CoreData/NSPersistentContainer/newBackgroundContext())

Returns a new managed object context that executes on a private queue.

[`viewContext`](/documentation/CoreData/NSPersistentContainer/viewContext)

The main queue’s managed object context.

### Performing Background Tasks

[`-  performBackgroundTask:`](/documentation/CoreData/NSPersistentContainer/performBackgroundTask(_:)-39sch)

Executes a closure on a private queue using an ephemeral managed object context.

[`performBackgroundTask(_:)`](/documentation/CoreData/NSPersistentContainer/performBackgroundTask(_:)-25nok)

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Inherited By

[`NSPersistentCloudKitContainer`](/documentation/CoreData/NSPersistentCloudKitContainer)

---

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)