Просмотр исходного кода

shift storage operations off from main thread, only update vars on main thread

polscm32 aka Marvout 1 год назад
Родитель
Сommit
5d41a5c8dc

+ 5 - 5
FreeAPS/Sources/Modules/Home/HomeDataFlow.swift

@@ -7,11 +7,11 @@ enum Home {
 
 protocol HomeProvider: Provider {
     func heartbeatNow()
-    func pumpSettings() -> PumpSettings
-    func autotunedBasalProfile() -> [BasalProfileEntry]
-    func basalProfile() -> [BasalProfileEntry]
+    func pumpSettings() async -> PumpSettings
+    func autotunedBasalProfile() async -> [BasalProfileEntry]
+    func basalProfile() async -> [BasalProfileEntry]
     func tempTargets(hours: Int) -> [TempTarget]
-    func pumpReservoir() -> Decimal?
+    func pumpReservoir() async -> Decimal?
     func tempTarget() -> TempTarget?
-    func getBGTargets() -> BGTargets
+    func getBGTargets() async -> BGTargets
 }

+ 10 - 10
FreeAPS/Sources/Modules/Home/HomeProvider.swift

@@ -26,29 +26,29 @@ extension Home {
             tempTargetsStorage.current()
         }
 
-        func pumpSettings() -> PumpSettings {
-            storage.retrieve(OpenAPS.Settings.settings, as: PumpSettings.self)
+        func pumpSettings() async -> PumpSettings {
+            await storage.retrieveAsync(OpenAPS.Settings.settings, as: PumpSettings.self)
                 ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
                 ?? PumpSettings(insulinActionCurve: 10, maxBolus: 10, maxBasal: 2)
         }
 
-        func pumpReservoir() -> Decimal? {
-            storage.retrieve(OpenAPS.Monitor.reservoir, as: Decimal.self)
+        func pumpReservoir() async -> Decimal? {
+            await storage.retrieveAsync(OpenAPS.Monitor.reservoir, as: Decimal.self)
         }
 
-        func autotunedBasalProfile() -> [BasalProfileEntry] {
-            storage.retrieve(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
+        func autotunedBasalProfile() async -> [BasalProfileEntry] {
+            await storage.retrieveAsync(OpenAPS.Settings.profile, as: Autotune.self)?.basalProfile
                 ?? storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
                 ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
         }
 
-        func basalProfile() -> [BasalProfileEntry] {
-            storage.retrieve(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
+        func basalProfile() async -> [BasalProfileEntry] {
+            await storage.retrieveAsync(OpenAPS.Settings.pumpProfile, as: Autotune.self)?.basalProfile
                 ?? [BasalProfileEntry(start: "00:00", minutes: 0, rate: 1)]
         }
 
-        func getBGTargets() -> BGTargets {
-            storage.retrieve(OpenAPS.Settings.bgTargets, as: BGTargets.self)
+        func getBGTargets() async -> BGTargets {
+            await storage.retrieveAsync(OpenAPS.Settings.bgTargets, as: BGTargets.self)
                 ?? BGTargets(from: OpenAPS.defaults(for: OpenAPS.Settings.bgTargets))
                 ?? BGTargets(units: .mgdL, userPreferredUnits: .mgdL, targets: [])
         }

+ 32 - 23
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -163,13 +163,13 @@ extension Home {
                         self.setupBatteryArray()
                     }
                     group.addTask {
-                        self.setupPumpSettings()
+                        await self.setupPumpSettings()
                     }
                     group.addTask {
-                        self.setupBasalProfile()
+                        await self.setupBasalProfile()
                     }
                     group.addTask {
-                        self.setupGlucoseTargets()
+                        await self.setupGlucoseTargets()
                     }
                     group.addTask {
                         self.setupReservoir()
@@ -464,32 +464,35 @@ extension Home {
             return roundedAmount.formatted()
         }
 
-        private func setupPumpSettings() {
-            DispatchQueue.main.async { [weak self] in
-                guard let self = self else { return }
-                self.maxBasal = self.provider.pumpSettings().maxBasal
+        private func setupPumpSettings() async {
+            let maxBasal = await provider.pumpSettings().maxBasal
+            await MainActor.run {
+                self.maxBasal = maxBasal
             }
         }
 
-        private func setupBasalProfile() {
-            DispatchQueue.main.async { [weak self] in
-                guard let self = self else { return }
-                self.autotunedBasalProfile = self.provider.autotunedBasalProfile()
-                self.basalProfile = self.provider.basalProfile()
+        private func setupBasalProfile() async {
+            let autotunedBasalProfile = await provider.autotunedBasalProfile()
+            let basalProfile = await provider.basalProfile()
+            await MainActor.run {
+                self.autotunedBasalProfile = autotunedBasalProfile
+                self.basalProfile = basalProfile
             }
         }
 
-        private func setupGlucoseTargets() {
-            DispatchQueue.main.async { [weak self] in
-                guard let self = self else { return }
-                self.bgTargets = self.provider.getBGTargets()
+        private func setupGlucoseTargets() async {
+            let bgTargets = await provider.getBGTargets()
+            await MainActor.run {
+                self.bgTargets = bgTargets
             }
         }
 
         private func setupReservoir() {
-            DispatchQueue.main.async { [weak self] in
-                guard let self = self else { return }
-                self.reservoir = self.provider.pumpReservoir()
+            Task {
+                let reservoir = await provider.pumpReservoir()
+                await MainActor.run {
+                    self.reservoir = reservoir
+                }
             }
         }
 
@@ -612,16 +615,22 @@ extension Home.StateModel:
     }
 
     func pumpSettingsDidChange(_: PumpSettings) {
-        setupPumpSettings()
-        setupBatteryArray()
+        Task {
+            await setupPumpSettings()
+            setupBatteryArray()
+        }
     }
 
     func basalProfileDidChange(_: [BasalProfileEntry]) {
-        setupBasalProfile()
+        Task {
+            await setupBasalProfile()
+        }
     }
 
     func bgTargetsDidChange(_: BGTargets) {
-        setupGlucoseTargets()
+        Task {
+            await setupGlucoseTargets()
+        }
     }
 
     func pumpReservoirDidChange(_: Decimal) {