SampleData on Previews @MainActor issue

Hi all,

I am trying to render my SwiftUI views that uses SwiftData classes using sample data using the approach shown in the example code of wwdc2023-10196:

@MainActor #Preview {
    TripsWidgetEntryView()
        .modelContainer(PreviewSampleData.container)
}

Unfortunately this seems no longer valid. Indeed I get this error:

I then tried to remove the @MainActor as suggested, but the error in then moved to another level:

What do you suggest to be the best approach to have back my preview working?

I am using Xcode Beta 4 - 15A5195m

Follow the recommendation - Click Fix.

Yes, that's what I did. I removed and the second error appeared.

Hi,

Sorry to hear you are having problems getting previews working with SwiftData. You are hitting a known issue. A workaround you can try is to wrap your preview content in MainActor.assumeIsolated { ... } like the suggestion here: https://developer.apple.com/forums/thread/731134?answerId=755431022#755431022

The sample has been updated. Please re-download it and try again with Xcode 15.0 beta 5 (15A5209g).

SampleData on Previews @MainActor issue
 
 
Q