---
title: ModelContainer
framework: swiftdata
role: symbol
role_heading: Class
path: swiftdata/modelcontainer
---

# ModelContainer

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

## Declaration

```swift
class ModelContainer
```

## Mentioned in

Reverting data changes using the undo manager Syncing model data across a person’s devices

## Overview

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() 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. 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 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. 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. @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:)](swiftdata/modelcontainer/init(for:migrationplan:configurations:)-1czix.md)
- [init(for:migrationPlan:configurations:)](swiftdata/modelcontainer/init(for:migrationplan:configurations:)-8s4ts.md)
- [init(for:migrationPlan:configurations:)](swiftdata/modelcontainer/init(for:migrationplan:configurations:)-qof9.md)
- [PersistentModel](swiftdata/persistentmodel.md)
- [ModelConfiguration](swiftdata/modelconfiguration.md)
- [Schema](swiftdata/schema.md)
- [SchemaMigrationPlan](swiftdata/schemamigrationplan.md)

### Managing schema and configuration details

- [schema](swiftdata/modelcontainer/schema.md)
- [configurations](swiftdata/modelcontainer/configurations.md)
- [migrationPlan](swiftdata/modelcontainer/migrationplan.md)

### Accessing the context

- [mainContext](swiftdata/modelcontainer/maincontext.md)

### Deleting the container

- [deleteAllData()](swiftdata/modelcontainer/deletealldata().md)

### Initializers

- [init(for:configurations:)](swiftdata/modelcontainer/init(for:configurations:)-621ei.md)
- [init(for:configurations:)](swiftdata/modelcontainer/init(for:configurations:)-93ifi.md)

### Instance Methods

- [erase()](swiftdata/modelcontainer/erase().md)

## Relationships

### Conforms To

- [Equatable](swift/equatable.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Model life cycle

- [ModelContext](swiftdata/modelcontext.md)
- [Fetching and filtering time-based model changes](swiftdata/fetching-and-filtering-time-based-model-changes.md)
- [HistoryDescriptor](swiftdata/historydescriptor.md)
- [Deleting persistent data from your app](swiftdata/deleting-persistent-data-from-your-app.md)
- [Reverting data changes using the undo manager](swiftdata/reverting-data-changes-using-the-undo-manager.md)
- [Syncing model data across a person’s devices](swiftdata/syncing-model-data-across-a-persons-devices.md)
- [Concurrency support](swiftdata/concurrencysupport.md)
