@malc Interesting idea. I don't think I'm initing twice. A SwiftData engineer acknowledged the corrupted modelContext environment variable as a known bug.
Container initialized in a singleton...
@MainActor struct DataCenter {
static var shared:DataCenter = DataCenter.init()
public let container: ModelContainer
public let context: ModelContext
init() {
let schema = Schema([
Folder.self,
Reminder.self,
DateReminder.self,
LocationReminder.self
])
container = try! ModelContainer(for: schema, configurations: [ModelConfiguration(isStoredInMemoryOnly: false)])
context = container.mainContext
}
}
Setting the container...
WindowGroup {
ContentView()
}
.modelContainer(DataCenter.shared.container)
I don't reference the modelContext environment variable anywhere. In code that needs the context, I always reference the singleton...
let folder = Folder(name: newFolderName)
DataCenter.shared.context.insert(folder)
It's been working fine, until iOS 18 came along. But I think my many problems with SwiftData in 18 have nothing at all to do with this container usage. But I've been wrong before.