Post

Replies

Boosts

Views

Activity

Reply to SwiftUI SwiftData Previews not working as expected
Looks like an @Model must be inserted into the context before use in SwiftUI actor PreviewSampleData { @MainActor static var container: ModelContainer = { let schema = Schema([Trip.self, BucketListItem.self, LivingAccommodation.self]) let configuration = ModelConfiguration(inMemory: true) let container = try! ModelContainer(for: schema, configurations: [configuration]) let sampleData: [any PersistentModel] = [ Trip.preview, BucketListItem.preview, LivingAccommodation.preview ] sampleData.forEach { container.mainContext.insert($0) } return container }() @MainActor static var previewTrip: Trip = { let container = PreviewSampleData.container let trip = Trip.preview container.mainContext.insert(trip) return trip }() } #Preview { MainActor.assumeIsolated { List { TripListItem(trip: PreviewSampleData.previewTrip) } .modelContainer(PreviewSampleData.container) } }
Jun ’23