I found some working solutions. This is how I generate the preview containter:
let previewContainer: ModelContainer = {
do {
let container = try ModelContainer(for: Snippet.self,
configurations: ModelConfiguration(isStoredInMemoryOnly: true))
Task { @MainActor in
let context = container.mainContext
let snip = Snippet.example2()
let folder = Folder(name: "folder with favorite snippet")
context.insert(folder)
folder.snippets.append(Snippet(isFavorite: true,
title: "favorite snippet"))
// add test data here
}
return container
} catch {
fatalError("Failed to create container: \(error.localizedDescription)")
}
}()
#Preview {
FolderListView()
.modelContainer(previewContainer)
}
Using view where I need to pass in a SwiftData object is more complex. I created a demo project with more examples. All previews are working for Xcode 15 https://github.com/gahntpo/SnippetBox-SwiftData