SwiftUI/SwiftData view deep in the hierarchy looses model container

iPad mini device with iPadOS 17.4.1.

I get this failure on iPad mini device exclusively. I have universal app and I doesn't get this error neither on iPhone or any simulator or macOS. I tried to reset the iPad, still getting the error.

The error happens when I send the app to background from the particular app screen.

Here is the error:

error: Store failed to load.
<NSPersistentStoreDescription: 0x3004b4bd0> (type: SQLite, url: file:///dev/null) with error = Error Domain=NSCocoaErrorDomain Code=134060 

"A Core Data error occurred."
UserInfo={NSLocalizedFailureReason=The configuration named 'default' does not contain any entities.} with userInfo {
    NSLocalizedFailureReason = "The configuration named 'default' does not contain any entities.";
}

What error says is that it loaded the store from file:///dev/null, which is wrong. Basically it looses the model container which is added via modelContaner modificator to the root view.

The error get caused particularly by the @Environment(\.modelContext) private var modelContext call in the view on which the failure occurs. This view is deep in the view hierarchy, and it get's created in the navigationDestination block.

I fixed the error by supplying modelContainer one more time right on the view:

.navigationDestination(for: File.self) { file in
    FileEditor(file: file)
        .modelContainer(FolderService.modelContainer)
}

I wonder, why can it loose the model container which is supplied on the root view?

Seeing the same bug on 17.4.1 iPads too. Can't reproduce on Mac or iPhone

I found that I had to remove ALL usage of @Environment(.modelContext) private var modelContext to eliminate all SwiftUI/SwiftData background crashes since 17.4. See https://developer.apple.com/forums/thread/744194?answerId=788019022#788019022

Can you show how you are setting the container on the root view and how you are initializing your container. Maybe you are accidentally initing it twice.

@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.

SwiftUI/SwiftData view deep in the hierarchy looses model container
 
 
Q