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?

From your code example, I don't see you creating a model container (ModelContainer) for your app – If that is the case, the error message will be exactly right, and it reminds you that the environment of ContentSheetView doesn't a model context, which is needed for Query to work, and yes, creating a valid model context and injecting it to the environment of ContentSheetView can fix the issue.

I can't explain why removing the if fault { Text("Fault!") } line has the described impact. If you can provide runnable code needed to reproduce the issue, I'd be intersted in taking a look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The modelContainer is being set in App. Here's the full code - it's mostly the default boilerplate:

And the log of what happens when entries have been added on MacOS (Designed for iPad) with when SQLDebug is turned on:

Data is already in the model container from an earlier run, so you can see the existing modelcontainer database is being opened correctly when the App is started, then when the sheet is opened, a new database is created at /dev/null for some reason.

The UI will correctly display the existing data in the sheet eventually, but having to create the database every time can really slow things down.

I'm targetting iOS 26 and running on MacOS 26.1. It doesn't happen on the iPhone however and removing the if statement in the sheet seems to make it go away on MacOS.

SwiftUI Sheet view with @Query loses model context
 
 
Q