SwiftUI SwiftData Previews not working as expected

re: the SwiftData session "Create an app with SwifData" (https://developer.apple.com/videos/play/wwdc2023/10154)

I corrected the @MainActor issue, but it seems to only work with the main ContentView and not other views.

I get the following error for TripListItem for example :

failed to find a currently active container for Trip

Answered by mrhanlon in 755500022

I was still running into this and similar errors getting SwiftUI views with SwiftData to preview successfully. It seems to me that you need to instantiate the container before you can reference the SwiftData objects in the container. I was finally able to solve it with the following:

#Preview {
    MainActor.assumeIsolated {
        let container = PreviewSampleData.container
        
        return List {
            TripListItem(trip: Trip.preview)
                .modelContainer(container)
        }
    }
}

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)
    }
}
Accepted Answer

I was still running into this and similar errors getting SwiftUI views with SwiftData to preview successfully. It seems to me that you need to instantiate the container before you can reference the SwiftData objects in the container. I was finally able to solve it with the following:

#Preview {
    MainActor.assumeIsolated {
        let container = PreviewSampleData.container
        
        return List {
            TripListItem(trip: Trip.preview)
                .modelContainer(container)
        }
    }
}

Interesting! not sure why I didn't try this 🙃

Hello, im still having this issue at Xcode beta 5, and none of the above workarounds does work either.

My #Preview Code:

#Preview {
    MainActor.assumeIsolated {
        let modelContainer: ModelContainer = ModelContainer.preview()
        return TaskListView()
            .modelContainer(modelContainer)
    }
}

My ModelContainer.preview() Code:

#if DEBUG
@MainActor
extension ModelContainer {
    static func preview() -> ModelContainer {
        do {
            let modelContainer = try ModelContainer(for: [TaskListModel.self], ModelConfiguration(inMemory: true))
            for element in [TaskListModel].preview() {
                modelContainer.mainContext.insert(element)
            }
            return modelContainer
        } catch let error {
            fatalError("[ModelContainer] static preview(taskLists:): \(error.localizedDescription)")
        }
    }
}
#endif
SwiftUI SwiftData Previews not working as expected
 
 
Q