SwiftData crash when switching between Window and ImmersiveSpace in visionOS

Environment

visionOS 26

Xcode 26

Issue

I am experiencing crash when trying to access a [String] from a @Model data, after dismissing an immersiveSpace and opening a WindowGroup.

This crash only occurs when trying to access the [String] property of my Model. It works fine with other properties.

Thread 1: Fatal error: This backing data was detached from a context without resolving attribute faults: PersistentIdentifier(...)

Steps to Reproduce

  • Open WindowGroup
  • Dismiss window, open ImmersiveSpace
  • Dismiss ImmersiveSpace, reopen WindowGroup

Any guidance would be appreciated!


@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup(id: "main") {
            ContentView()
        }
        .modelContainer(for: [Item.self])
        
        ImmersiveSpace(id: "immersive") {
            ImmersiveView()
        }
    }
}

// In SwiftData model
@Model
class Item {
    var title: String = "" // Accessing this property works fine
    var tags: [String] = []

    @storageRestrictions(accesses: _$backingData, initializes: _tags)

    init(initialValue) {
        _$backingData.setValue(forKey: \. tags, to: initialValue)
        _tags =_ SwiftDataNoType()
    }

    get {
        _$observationRegistrar.access(self, keyPath: \.tags)
        **return self getValue(forkey: \.tags)** // Crashes here
    }

Would you mind to provide a runnable code snippet with detailed steps that reproduce the issue? I'd try to figure out what happens.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Unfortunately, it is quite difficult to share any code snippet since it's a large project. Do you have any clue on what the error means and what could cause it?

Nothing I can say for sure unfortunately.

The error message seems to indicate that the model container was changed and so the model context lost the link to a back store. (That can't explain why accessing title is fine however.)

You can probably try with creating a (singleton) model container shared across the whole app and passing it to your window group with .modelContainer(sharedModelContainer) to see if that changes anything.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData crash when switching between Window and ImmersiveSpace in visionOS
 
 
Q