0

I am using the new SwiftData to persist the data into the database. For testing purposes, I have created fake data for UI preview as follows:

@MainActor
let modelTestContainer: ModelContainer = {
    do {
        let container = try ModelContainer(
            for: Interest.self, ModelConfiguration(inMemory: true)
        )
        for interest in InterestSample.seed {
            container.mainContext.insert(object: interest)
        }
        return container
    } catch {
        fatalError("Failed to create container")
    }
}()

The UI code compiles but the preview is failing with the following message:

Compiling failed: main actor-isolated let 'modelTestContainer' can not be referenced from a non-isolated context 

Here is the UI code:

import SwiftUI
import SwiftData

struct Overview: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var interests: [Interest]
    
    var body: some View {
        NavigationView {
            List {
                ForEach(interests) { item in
                    NavigationLink {
                        Text("Item at \(item.createdAt, format: Date.FormatStyle(date: .numeric, time: .standard))")
                    } label: {
                        Text(item.createdAt, format: Date.FormatStyle(date: .numeric, time: .standard))
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                Button("new") {
                    print("save document")
                }
                Button("publish") {
                    print("save document")
                }
            }
        }
    }
    
    private func addNewInterests() {
        withAnimation {
            //modelContext.insert(newItem)
        }
    }
    
    private func publishInterests() {
        
    }
    
    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            for index in offsets {
                modelContext.delete(interests[index])
            }
        }
    }
}

#Preview {
    Overview()
        .modelContainer(modelTestContainer)
}

Why the preview does not work?

softshipper
  • 32,463
  • 51
  • 192
  • 400

2 Answers2

0

This is a bug and the workaround is to tell the compiler to assume it is isolated using

MainActor.assumeIsolated

So

#Preview {
    MainActor.assumeIsolated
        Overview()
            .modelContainer(modelTestContainer)
    }
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

I'm guessing from your timing you were using beta 2 of Xcode 15.

There was/is a bug.

The Release Notes for Xcode 15 (this link will likely change content with subsequent releases) beta 3 states:

Previewing view code in a SwiftData app results in a linker failure for the canvas despite that code compiling successfully for device and simulator. (111657477) Workaround: Run your app in the simulator to test your UI instead of using Previews.

There are additional comments for a variety of Preview issues.

bshirley
  • 8,217
  • 1
  • 37
  • 43