Contents

modelContainer(_:)

Sets the model container and associated model context in this view’s environment.

Declaration

@MainActor @preconcurrency func modelContainer(_ container: ModelContainer) -> some View

Parameters

  • container:

    The model container to use for this view.

Discussion

In this example, ContentView sets a model container to use for RecipesList:

struct ContentView: View {
    @State private var container = ModelContainer(...)

    var body: some Scene {
        RecipesList()
            .modelContainer(container)
    }
}

The environment’s modelContext property will be assigned a new context associated with this container. All implicit model context operations in this view, such as Query properties, will use the environment’s context.

See Also

Configuring a model