So I have two class models, Item and Category, as follows:
@Model
class Category {
let name: String
...
}
@Model
class Item {
var name: String
var category: Category?
...
}
I have a view called ItemDetailsView that displays all the properties of a given item. It also has a toggle that shows an ItemEditorView where user's can edit the item properties, including the category which shows up in a picker.
I set up the preview for ItemDetailsView as follows:
#Preview {
let preview = Preview(Item.self, Category.self)
preview.addExamples(Category.sampleCategories)
preview.addExamples(Item.sampleItems)
return ItemDetailsView(item: Item.sampleItems[0])
.modelContainer(preview.container)
}
The Preview class sets up my model container and inserts the sample items and categories that I have instantiated as static constant variables.
struct Preview {
let container: ModelContainer
init(_ models: any PersistentModel.Type...) {
let schema = Schema(models)
let config = ModelConfiguration(isStoredInMemoryOnly: true)
do {
container = try ModelContainer(for: schema, configurations: config)
} catch {
fatalError("Could not create preview container")
}
}
func addExamples(_ examples: [any PersistentModel]) {
Task { @MainActor in
examples.forEach { example in
container.mainContext.insert(example)
}
}
}
}
I'm not sure why my preview is crashing. I had a hunch that it was because we needed all the categories to be available for the picker in ItemEditorView but I already inserted the relevant categories into the model context. Preview is working as expected in a different view called ItemDetailsView which lists all the items added. Navigation links and sheets work perfectly. Not sure why this is giving me trouble.