Xcode Preview crash with ModelContainer initialization
12:21 24 Dec 2025

Background:

I have complex Swift Data models with some Relationships, and at some point, my Xcode Preview is no longer working when I want to init a ModelContainer in it. So I tried to debug and create a simple file with no dependencies with other parts of my project.

Here's my simple code:

import Foundation
import SwiftUI
import SwiftData

struct DebugView: View {
    var body: some View {
        Text("Hello")
    }
}

@Model
class User {
    var name: String

    init(name: String) {
        self.name = name
    }
}

#Preview {
    let config = ModelConfiguration(UUID().uuidString, isStoredInMemoryOnly: true)
    let container = try! ModelContainer(for: User.self, configurations: config) // This line failed
    DebugView()
}

The thing is even this simple view cannot be shown in the Preview window. Error message:

enter image description here

I'm sure that the crash line is at the ModelContainer init because it works fine without it.

I tried to delete the cache data with command:

xcrun simctl --set previews delete all

But it's still not working.

The weird thing is, when I created a new project and move all the files in it, it works at the beginning. But it started to failed when I tried to do some modifications on other files (these simple codes stay unchanged).

Does any one have any idea?

xcode swiftui canvas preview swiftdata