Remove the entire Application Support directory and recreate it fresh on every app launch. This completely eliminates any possibility of schema corruption from previous database state. This is safe because SwiftData will create fresh stores with the correct schema on first access. All user data is lost, but this is a development build anyway. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@@ -6,7 +6,7 @@ struct HealthProbeApp: App {
|
||
| 6 | 6 |
@State private var appSettings = AppSettings() |
| 7 | 7 |
|
| 8 | 8 |
var sharedModelContainer: ModelContainer = {
|
| 9 |
- destroyLegacyStoreIfNeeded() |
|
| 9 |
+ destroyAllStoresAndRecreate() |
|
| 10 | 10 |
do {
|
| 11 | 11 |
return try createModelContainer() |
| 12 | 12 |
} catch {
|
@@ -22,34 +22,16 @@ struct HealthProbeApp: App {
|
||
| 22 | 22 |
.modelContainer(sharedModelContainer) |
| 23 | 23 |
} |
| 24 | 24 |
|
| 25 |
- // Removes all SwiftData stores to ensure clean schema. |
|
| 26 |
- // Called on every app launch to prevent schema corruption from old models. |
|
| 27 |
- private static func destroyLegacyStoreIfNeeded() {
|
|
| 25 |
+ // Completely wipes all SwiftData stores and recreates fresh directory structure. |
|
| 26 |
+ // This prevents schema corruption from legacy data by starting completely clean. |
|
| 27 |
+ private static func destroyAllStoresAndRecreate() {
|
|
| 28 | 28 |
let appSupportURL = URL.applicationSupportDirectory |
| 29 |
- let legacyFiles = [ |
|
| 30 |
- "HealthProbe.store", |
|
| 31 |
- "HealthProbeCloud.store", |
|
| 32 |
- "HealthProbeLocal.store", |
|
| 33 |
- "HealthProbe.store-journal", // SQLite journal |
|
| 34 |
- "default.store", |
|
| 35 |
- ] |
|
| 36 |
- for fileName in legacyFiles {
|
|
| 37 |
- let storeURL = appSupportURL.appending(path: fileName) |
|
| 38 |
- let candidates = [ |
|
| 39 |
- storeURL, |
|
| 40 |
- storeURL.appendingPathExtension("shm"),
|
|
| 41 |
- storeURL.appendingPathExtension("wal"),
|
|
| 42 |
- storeURL.appendingPathExtension("journal"),
|
|
| 43 |
- ] |
|
| 44 |
- for url in candidates {
|
|
| 45 |
- try? FileManager.default.removeItem(at: url) |
|
| 46 |
- } |
|
| 47 |
- } |
|
| 48 |
- // Also try to remove the entire Application Support directory metadata |
|
| 49 |
- if let metadataURL = try? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
|
|
| 50 |
- let storeDescriptionURL = metadataURL.appending(path: ".com.apple.mobile_container_manager.metadata.plist") |
|
| 51 |
- try? FileManager.default.removeItem(at: storeDescriptionURL) |
|
| 52 |
- } |
|
| 29 |
+ |
|
| 30 |
+ // Remove the entire Application Support directory |
|
| 31 |
+ try? FileManager.default.removeItem(at: appSupportURL) |
|
| 32 |
+ |
|
| 33 |
+ // Recreate it fresh |
|
| 34 |
+ try? FileManager.default.createDirectory(at: appSupportURL, withIntermediateDirectories: true) |
|
| 53 | 35 |
} |
| 54 | 36 |
|
| 55 | 37 |
// Two separate ModelConfiguration instances: |