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?

Add a Comment