Swift Data - fail gracefully if file can't be opened

Hello

By default if swiftData is unable to open a file on mac (due to it being the wrong format, or an old model) the app hard crashes (macOS 14.4.1) - is there a way to catch this problem and present a message to the user, rather than the app simply crashing

The whole way that swiftData opens files etc is very opaque so I'm not clear how we can wrap things in a nice way

Thanks

Richard

Answered by DTS Engineer in 793567022

SwiftData was designed to hide the details of loading a store. All the logics are wrapped in the model container creation. If the process hits an error, SwiftData throws an error, as shown below:

    func createModelContainer() -> ModelContainer? {
        var modelContainer: ModelContainer?
        do {
            modelContainer = try ModelContainer(for: ...)
        } catch {
            // Handle the error here.
        }
        return modelContainer
    }

Apps can detect and handle the error when creating a model container. Note that they should avoid using SwiftData (the model context, for example) before having a valid model container. For a SwiftUI app driven by SwiftData, I'd probably consider the following flow:

  1. Start the app with a SwiftUI view that does not touch any SwiftData types, other than creating the model container.

  2. Create the model container, as shown above, in the view's onAppear.

  3. If hitting an error at step 2, present a user message. Otherwise, load the view hierarchy as usual, with the model container.

This ensures that the app creates the model contianer and handles the error, if any, before using SwiftData.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData was designed to hide the details of loading a store. All the logics are wrapped in the model container creation. If the process hits an error, SwiftData throws an error, as shown below:

    func createModelContainer() -> ModelContainer? {
        var modelContainer: ModelContainer?
        do {
            modelContainer = try ModelContainer(for: ...)
        } catch {
            // Handle the error here.
        }
        return modelContainer
    }

Apps can detect and handle the error when creating a model container. Note that they should avoid using SwiftData (the model context, for example) before having a valid model container. For a SwiftUI app driven by SwiftData, I'd probably consider the following flow:

  1. Start the app with a SwiftUI view that does not touch any SwiftData types, other than creating the model container.

  2. Create the model container, as shown above, in the view's onAppear.

  3. If hitting an error at step 2, present a user message. Otherwise, load the view hierarchy as usual, with the model container.

This ensures that the app creates the model contianer and handles the error, if any, before using SwiftData.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Swift Data - fail gracefully if file can't be opened
 
 
Q