Contents

modelContext(_:)

Sets the model context in this scene’s environment.

Declaration

@MainActor @preconcurrency func modelContext(_ modelContext: ModelContext) -> some Scene

Parameters

  • modelContext:

    The model context to set in this scene’s environment.

Discussion

In this example, RecipesApp sets a shared model context to use for all of its windows:

@Model class Recipe { ... }

@main
struct RecipesApp: App {
    var body: some Scene {
        WindowGroup {
            RecipesList()
        }
        .modelContext(myContext)
    }
}

The environment’s modelContext property will be assigned a myContext. All implicit model context operations in this scene, such as Query properties, will use the environment’s context.

See Also

Configuring a data model