Showing 1 changed files with 21 additions and 19 deletions
+21 -19
HealthProbe/HealthProbeApp.swift
@@ -22,17 +22,24 @@ struct HealthProbeApp: App {
22 22
         .modelContainer(sharedModelContainer)
23 23
     }
24 24
 
25
-    // Removes the legacy store to avoid schema incompatibility.
26
-    // The old store used TypeDistributionBin which is no longer in the schema.
25
+    // Removes legacy stores to avoid schema incompatibility.
26
+    // Clears all previous database files that may have incompatible models.
27 27
     private static func destroyLegacyStoreIfNeeded() {
28
-        let storeURL = URL.applicationSupportDirectory.appending(path: "HealthProbe.store")
29
-        guard FileManager.default.fileExists(atPath: storeURL.path()) else { return }
30
-        let candidates = [
31
-            storeURL,
32
-            storeURL.appendingPathExtension("shm"),
33
-            storeURL.appendingPathExtension("wal"),
28
+        let appSupportURL = URL.applicationSupportDirectory
29
+        let legacyFiles = [
30
+            "HealthProbe.store",
31
+            "HealthProbeCloud.store",
32
+            "HealthProbeLocal.store",
34 33
         ]
35
-        for url in candidates { try? FileManager.default.removeItem(at: url) }
34
+        for fileName in legacyFiles {
35
+            let storeURL = appSupportURL.appending(path: fileName)
36
+            let candidates = [
37
+                storeURL,
38
+                storeURL.appendingPathExtension("shm"),
39
+                storeURL.appendingPathExtension("wal"),
40
+            ]
41
+            for url in candidates { try? FileManager.default.removeItem(at: url) }
42
+        }
36 43
     }
37 44
 
38 45
     // Two separate ModelConfiguration instances:
@@ -66,22 +73,17 @@ struct HealthProbeApp: App {
66 73
             cloudKitDatabase: .none
67 74
         )
68 75
 
69
-        #if targetEnvironment(simulator)
76
+        // Use local storage for now. CloudKit sync requires the container to be
77
+        // set up in Apple Developer Portal first. To enable CloudKit:
78
+        // 1. Create a CloudKit container "iCloud.ro.xdev.HealthProbe" in Developer Portal
79
+        // 2. Enable iCloud capability for this app
80
+        // 3. Replace .none with .private("iCloud.ro.xdev.HealthProbe")
70 81
         cloudConfig = ModelConfiguration(
71 82
             "cloud",
72 83
             schema: cloudKitModels,
73 84
             url: appSupportURL.appending(path: "HealthProbeCloud.store"),
74 85
             cloudKitDatabase: .none
75 86
         )
76
-        #else
77
-        // For production, set up the CloudKit container in Apple Developer Portal.
78
-        // Container must be: "iCloud." + PRODUCT_BUNDLE_IDENTIFIER
79
-        cloudConfig = ModelConfiguration(
80
-            "cloud",
81
-            schema: cloudKitModels,
82
-            cloudKitDatabase: .private("iCloud.ro.xdev.HealthProbe")
83
-        )
84
-        #endif
85 87
 
86 88
         let fullSchema = Schema([
87 89
             HealthSnapshot.self, TypeCount.self, YearlyCount.self,