SwiftUI Sheet view with @Query loses model context

I've run into a strange issue.

If a sheet loads a view that has a SwiftData @Query, and there is an if statement in the view body, I get the following error when running an iOS targetted SwiftUI app under MacOS 26.1:

Set a .modelContext in view's environment to use Query

While the view actually ends up loading the correct data, before it does, it ends up re-creating the sqlite store (opening as /dev/null).

The strange thing is that this only happens if there is an if statement in the body. The statement need not ever evaluate true, but it causes the issue.

Here's an example. It's based on the default xcode new iOS project w/ SwiftData:

struct ContentView: View {
    @State private var isShowingSheet = false

    var body: some View {
        Button(action: { isShowingSheet.toggle() }) {
            Text("Show Sheet")
        }        
        .sheet(isPresented: $isShowingSheet, onDismiss: didDismiss) {            
            VStack {
                ContentSheetView()
            }
        }
    }

    func didDismiss() { }
}

struct ContentSheetView: View {   
    @Environment(\.modelContext) private var modelContext
    @Query public var items: [Item]    
    @State var fault: Bool = false    
    var body: some View {        
        VStack {
            if fault { Text("Fault!")  }

            Button(action: addItem) {
                Label("Add Item", systemImage: "plus")
            }

            List {
                ForEach(items) { item in 
                    Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
                }
            }
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(timestamp: Date()) 
            modelContext.insert(newItem)
        }    
    }
}

It requires some data to be added to trigger, but after adding it and dismissing the sheet, opening up the sheet with trigger the Set a .modelContext in view's environment to use Query. Flipping on -com.apple.CoreData.SQLDebug 1 will show it trying to recreate the database.

If you remove the if fault { Text("Fault!") } line, it goes away. It also doesn't appear to happen on iPhones or in the iPhone simulator.

Explicitly passing modelContext to the ContentSheetView like ContentSheetView().modelContext(modelContext) also seems to fix it.

Is this behavior expected?

SwiftUI Sheet view with @Query loses model context
 
 
Q