modelContainer in SwiftData

Hello, In SwiftUI within Swift Playgrounds on iPad, I get two different results depending on whether I place .modelContainer(for: Valeur.self) immediately after ContentView() or after WindowGroup {....}.

If I place it after WindowGroup {....}, I can neither save to nor query the database in the preview; it only works in read-only mode.

If I place it after ContentView(), it works correctly in both preview and read-write mode.

Which version should I use? Thanks for your feedback.

For the app itself, I would put .modelContainer(for: Valeur.self) after WindowGroup. That is Apple’s documented setup and gives all windows in the group the same container and model context.

A preview can use its own in-memory container for testing. For example, attach .modelContainer(for: Valeur.self, inMemory: true) to the preview’s ContentView. Changes in that preview are temporary. Putting the modifier on ContentView() in the app also works, but scopes that container to that view subtree instead of the whole window group.

If the WindowGroup version is read-only in your Playgrounds preview, could you share the small app declaration and how the preview is configured? The preview may be using a different model context. Apple’s ModelContainer documentation includes the WindowGroup pattern and a link to its preview example.