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

+ 7 - 2
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -688,8 +688,13 @@ extension Bolus.StateModel {
     }
 
     @MainActor func updateForecasts() async {
-        simulatedDetermination = await apsManager.simulateDetermineBasal(carbs: carbs, iob: amount)
-        predictionsForChart = simulatedDetermination?.predictions
+        // Run simulateDetermineBasal on a background thread
+        let result = await Task.detached { [self] in
+            await apsManager.simulateDetermineBasal(carbs: carbs, iob: amount)
+        }.value
+
+        simulatedDetermination = result
+        predictionsForChart = result?.predictions
 
         let iob: [Int] = predictionsForChart?.iob ?? []
         let zt: [Int] = predictionsForChart?.zt ?? []

+ 27 - 25
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -951,31 +951,33 @@ extension Home.StateModel {
     }
 
     // Fetch forecast values for a given data set
-       func fetchForecastValues(
-           for data: (id: UUID, forecastID: NSManagedObjectID, forecastValueIDs: [NSManagedObjectID]),
-           in context: NSManagedObjectContext
-       ) async -> (UUID, Forecast?, [ForecastValue]) {
-           // Initialize the variables for the forecast and forecast values
-           var forecast: Forecast?
-           var forecastValues: [ForecastValue] = []
-           
-           do {
-               // Try to fetch the forecast object
-               forecast = try context.existingObject(with: data.forecastID) as? Forecast
-
-               // Fetch the forecast values, limiting to the first 36 values, i.e. 3 hours from the current time
-               for forecastValueID in data.forecastValueIDs.prefix(36) {
-                   if let forecastValue = try context.existingObject(with: forecastValueID) as? ForecastValue {
-                       forecastValues.append(forecastValue)
-                   }
-               }
-           } catch {
-               debugPrint("\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to fetch forecast Values with error: \(error.localizedDescription)")
-           }
-
-           return (data.id, forecast, forecastValues)
-       }
-    
+    func fetchForecastValues(
+        for data: (id: UUID, forecastID: NSManagedObjectID, forecastValueIDs: [NSManagedObjectID]),
+        in context: NSManagedObjectContext
+    ) async -> (UUID, Forecast?, [ForecastValue]) {
+        // Initialize the variables for the forecast and forecast values
+        var forecast: Forecast?
+        var forecastValues: [ForecastValue] = []
+
+        do {
+            // Try to fetch the forecast object
+            forecast = try context.existingObject(with: data.forecastID) as? Forecast
+
+            // Fetch the forecast values, limiting to the first 36 values, i.e. 3 hours from the current time
+            for forecastValueID in data.forecastValueIDs.prefix(36) {
+                if let forecastValue = try context.existingObject(with: forecastValueID) as? ForecastValue {
+                    forecastValues.append(forecastValue)
+                }
+            }
+        } catch {
+            debugPrint(
+                "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to fetch forecast Values with error: \(error.localizedDescription)"
+            )
+        }
+
+        return (data.id, forecast, forecastValues)
+    }
+
     // Update forecast data and UI on the main thread
     @MainActor func updateForecastData() async {
         // Preprocess forecast data on a background thread