fix CoreData store crash: create Application Support dir, explicit local config
The Application Support directory doesn't always exist on fresh installs. Create it explicitly before configuring ModelContainer. Use explicit store URL and cloudKitDatabase: .none to prevent any CloudKit/remote notification options from being added. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,10 +3,28 @@ import SwiftUI
|
||||
|
||||
@main
|
||||
struct VoiceDiaryApp: App {
|
||||
let modelContainer: ModelContainer
|
||||
|
||||
init() {
|
||||
// Ensure Application Support directory exists
|
||||
let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
|
||||
try? FileManager.default.createDirectory(at: appSupport, withIntermediateDirectories: true)
|
||||
|
||||
let schema = Schema([DiaryEntry.self, VoiceMemo.self])
|
||||
let storeURL = appSupport.appendingPathComponent("VoiceDiary.store")
|
||||
let config = ModelConfiguration("VoiceDiary", schema: schema, url: storeURL, cloudKitDatabase: .none)
|
||||
|
||||
do {
|
||||
modelContainer = try ModelContainer(for: schema, configurations: [config])
|
||||
} catch {
|
||||
fatalError("Failed to create ModelContainer: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
.modelContainer(for: [DiaryEntry.self, VoiceMemo.self])
|
||||
.modelContainer(modelContainer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user