Xcode preview crashes all the time

Hello together, I'm currently learning CoreData, but my preview in Xcode crashes always when I try to add something new to the view. Does someone have an idea what I need to pass to the preview that it works?

My code

Replies

Hi,

Sorry to hear you are having problems getting previews working. Could you be hitting this issue listed in the release notes?

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.

If not, the best next step will be to file a feedback with diagnostics so we can take a look.

Steps to generate helpful diagnostics:

  1. Download and install the logging profile on all devices involved. Instructions and profiles are available here: https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift
  2. Reproduce the issue
  3. Click the "Diagnostics" button in the error banner in Previews' Canvas area (or if the banner is missing you can use the menu: Editor > Canvas > Diagnostics)
  4. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  5. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
  6. Generate the sysdiagnose(s) and attach those too
  • I sincerely believe that the best way to solve a problem is to provide a step-by-step guide or sample code that can 100% reproduce the issue, rather than diagnostic data.

    Although I encountered a different issue, I noticed that every time it was related to Preview, the request was always for diagnostic data feedback. I believe this is not very efficient.

  • Thanks for your feedback! Great idea! A bug report containing all 3 would be even more valuable and increase the odds that it becomes actionable right away, so I do encourage you to file a feedback with as much helpful information as possible, but please do include the diagnostics as it helps answer a lot of questions that a lot of feedback authors may not initially think is important!

Add a Comment

Hi Thank you all for your swift response and great suggestions.

But I guess the problem isn't Xcode or the preview. I think it has something to do that I dont't pass the CoreData instance the right way. I tried once with the ".environment" but it didn't work. Does someone have an idea?

    @Environment(\.managedObjectContext) var moc
    
    static var previews: some View {
        AddListView()
            .environment(.managedObjectContext, DataController(name: "preview").container.viewContext))
    }
}

This is how the crash looks like: