This post is from the WWDC26 SwiftUI Q&A.
Is this the right way to pass data to a sheet for editing?
struct Detail: View {
...
// The single atomic source of truth for our sheet presentation
@State var editorConfig: EditorConfig?
// Completely encapsulated local configuration package
struct EditorConfig: Identifiable {
var id: PersistentIdentifier {
item.persistentModelID
}
let context: ModelContext
let item: Item
}
var body: some View {
...
Button("Edit") {
// 1. Spin up a separate scratchpad container layer
let context = ModelContext(modelContext.container)
context.autosaveEnabled = false
// 2. Safely resolve our model inside the new isolated playground
if let sandboxItem = context.model(for: item.persistentModelID) as? Item {
// 3. Package it up to trigger the sheet presentation
editorConfig = EditorConfig(context: context, item: sandboxItem)
}
}
}
}
// 4. SwiftUI tracks value replacement accurately without ghost state bugs
.sheet(item: $editorConfig) { config in
// Inject the isolated context into the sheet's environment chain
EditorView(item: config.item)
.environment(\.modelContext, config.context)
}
}
}