Contents

modelContext(_:)

Sets the model context in this view’s environment.

Declaration

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

Parameters

  • modelContext:

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

Discussion

In this example, the RecipesList view sets a model context to use for all of its content:

@Model class Recipe { ... }
...
RecipesList()
    .modelContext(myContext)

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

See Also

Configuring a model