<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "swift: 5.9.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftData",
  "identifier" : "/documentation/SwiftData/ModelContainer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "SwiftData"
    ],
    "preciseIdentifier" : "s:9SwiftData14ModelContainerC"
  },
  "title" : "ModelContainer"
}
-->

# ModelContainer

An object that manages an app’s schema and model storage configuration.

```
class ModelContainer
```

## Overview

A model container mediates between its associated model contexts and your app’s
underlying persistent storage. The container manages all aspects of that
storage and ensures it remains in a consistent and usable state. Whenever you
run a fetch or call a context’s [`save()`](/documentation/SwiftData/ModelContext/save()) method, the container
performs the actual read or write of the underlying data using information from
the schema you provide. This helps safeguard an app’s resources and ensures
those operations happen only in an efficient and coordinated manner.
Additionally, if your app’s entitlements include CloudKit, the container
automatically handles syncing the persisted storage across devices. For more
information about syncing model data, see [Syncing model data across a person’s devices](/documentation/SwiftData/Syncing-model-data-across-a-persons-devices).

As your app’s schema evolves, the container performs automatic migrations of
the persisted model data so it remains consistent with the app’s model classes.
If the aggregate changes between two versions of your schema exceed the
capabilities of automatic migrations, provide the container with a
[`SchemaMigrationPlan`](/documentation/SwiftData/SchemaMigrationPlan) to participate in those migrations and help ensure they
complete successfully.

By default, a model container makes a number of assumptions about how it
configures an app’s persistent storage. If you need to customize this behavior,
provide the container with one or more instances of [`ModelConfiguration`](/documentation/SwiftData/ModelConfiguration). For
example, you may want use a particular app group container or specify that the
storage is ephemeral and should exist only in memory.

An app that uses SwiftData requires at least one model container. You create a
container using one of the class’s initializers or the corresponding SwiftUI
view modifier. Using the view modifier ensures all windows in the modified
window group, or all child views of the modified view, access the same model
container. Additionally, the view modifier makes an associated model context
available in the SwiftUI environment, which the `Query()` macro depends on.

```swift
@main
struct RecipesApp: App {
    var body: some Scene {
        WindowGroup {
            RecipesList()
        }
        .modelContainer(for: Recipe.self)
    }
}

struct RecipesList: View {
    @Query private var recipes: [Recipe]
    
    var body: some View {
        List(recipes) { RecipeRowView($0) }
    }
} 
```

## Topics

### Creating a model container

[`init(for:migrationPlan:configurations:)`](/documentation/SwiftData/ModelContainer/init(for:migrationPlan:configurations:)-1czix)

Creates a model container using the specified schema, migration plan, and
configurations.

[`init(for:migrationPlan:configurations:)`](/documentation/SwiftData/ModelContainer/init(for:migrationPlan:configurations:)-8s4ts)

Creates a model container using the specified model types, migration plan, and
zero or more configurations.

[`init(for:migrationPlan:configurations:)`](/documentation/SwiftData/ModelContainer/init(for:migrationPlan:configurations:)-qof9)

Creates a model container using the specified schema, migration plan, and zero
or more configurations.

[`PersistentModel`](/documentation/SwiftData/PersistentModel)

An interface that enables SwiftData to manage a Swift class as a stored model.

[`ModelConfiguration`](/documentation/SwiftData/ModelConfiguration)

A type that describes the configuration of an app’s schema or specific group of models.

[`Schema`](/documentation/SwiftData/Schema)

An object that maps model classes to data in the model store, and helps with the migration of that data
between releases.

[`SchemaMigrationPlan`](/documentation/SwiftData/SchemaMigrationPlan)

An interface for describing the evolution of a schema and how to migrate between specific versions.

### Managing schema and configuration details

[`schema`](/documentation/SwiftData/ModelContainer/schema)

The schema that maps your app’s model classes to the associated data in the
app’s persistent storage.

[`configurations`](/documentation/SwiftData/ModelContainer/configurations)

The configurations that describe how to manage the persisted data for specific
groups of models.

[`migrationPlan`](/documentation/SwiftData/ModelContainer/migrationPlan)

The plan that describes the evolution of your app’s schema and how to migrate
between specific versions.

### Accessing the context

[`mainContext`](/documentation/SwiftData/ModelContainer/mainContext)

A model context that’s bound to app’s main actor.

### Deleting the container

[`deleteAllData()`](/documentation/SwiftData/ModelContainer/deleteAllData())

Removes all persisted model data from the app’s persistent storage.

### Comparing model containers



---

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)