DocumentGroup with SwiftData BUG!!!!! (modelContext cannot save and querry)

This is a critical bug with Document-Based Apps (SwiftData). If you download the WWDC 2023 sample code for"Building a document-based app using SwiftData" , open it in Xcode 16.1, and run it on an iOS 18+ simulator, you'll encounter a major issue. When you exit a document and reopen it, you'll find that the changes you just made were not saved.

iOS 18 has effectively rendered last year's WWDC 2023 sample code obsolete! Has anyone managed to successfully save data in a Document-Based App using SwiftData?

Indeed, I could reproduce this.

It seems that the ModelContext injectable via the @Environment in document based apps does not autosave. That is, one needs to manually call modelContext.save(). (This is contrary to non-document based apps, where the main context autosaves by default, see here: https://developer.apple.com/documentation/swiftdata/modelcontext/autosaveenabled).

One would need to look into the architecture of the DocumentGroup in detail, but my intuition would be that what you inject via the @Environment, in this case, is not a MainActor-bound ModelContext, but a Scene scoped one, since you can have multiple documents open at the same time.

DocumentGroup with SwiftData BUG!!!!! (modelContext cannot save and querry)
 
 
Q