The traditional way to initialize a SwiftData app looks like some variation of the following:
@main
struct TrainingCornerApp: App {
let dbSchema: Schema = ...
init() {
}
var body: some Scene {
WindowGroup {
MainAppScreen()
.modelContainer(modelContainer)
}
}
}
private var modelContainer: ModelContainer {
let container: ModelContainer
let modelConfiguration = ModelConfiguration(schema: dbSchema, isStoredInMemoryOnly: false, cloudKitDatabase: .automatic)
do {
container = try ModelContainer(for: dbSchema, migrationPlan: MigrationPlan.self, configurations: [modelConfiguration])
} catch {
fatalError("Failed to create model container: \(error.localizedDescription)")
}
return container
}
}
That is, if creating the modelContainer fails, the app aborts with a fatalError.
What I want to do is have the application present an alert message for the user before terminating. (Let the user know the app can't run and suggest how to get help -- that sort of thing.)
However, I've been having a hard time getting this to work, and it seems that the problems are related to trying to generate the alert at such an early stage in app startup.
Does anyone have ideas/suggestions on how I might do this?