ソースを参照

Merge branch 'dev' of github.com:nightscout/Trio into apsManager-part3

Marvin Polscheit 2 週間 前
コミット
d8fb6e14a2
2 ファイル変更27 行追加17 行削除
  1. 1 1
      Config.xcconfig
  2. 26 16
      Trio/Sources/APS/APSManager.swift

+ 1 - 1
Config.xcconfig

@@ -19,7 +19,7 @@ TRIO_APP_GROUP_ID = group.org.nightscout.$(DEVELOPMENT_TEAM).trio.trio-app-group
 
 
 // The developers set the version numbers, please leave them alone
 // The developers set the version numbers, please leave them alone
 APP_VERSION = 0.8.3
 APP_VERSION = 0.8.3
-APP_DEV_VERSION = 0.8.3.2
+APP_DEV_VERSION = 0.8.3.5
 APP_BUILD_NUMBER = 1
 APP_BUILD_NUMBER = 1
 COPYRIGHT_NOTICE =
 COPYRIGHT_NOTICE =
 
 

+ 26 - 16
Trio/Sources/APS/APSManager.swift

@@ -301,8 +301,18 @@ final class BaseAPSManager: APSManager, Injectable {
                 return
                 return
             }
             }
 
 
-            // Local background-task identifier — no shared mutable state.
-            let bgTask = await UIApplication.shared.beginBackgroundTask(withName: "Loop starting")
+            // Start background task
+            // we probably need to refactor this when implementing Swift 6 due to mutation of a captured var in an async context
+            var taskID: UIBackgroundTaskIdentifier = .invalid
+            taskID = await UIApplication.shared.beginBackgroundTask(withName: "Loop starting") {
+                // closure runs on the Main Thread
+                // removed the Task that provided no guarantee to end the background task
+                if taskID != .invalid {
+                    UIApplication.shared.endBackgroundTask(taskID)
+                    taskID = .invalid
+                }
+            }
+
             isLooping.send(true)
             isLooping.send(true)
 
 
             let loopStartDate = Date()
             let loopStartDate = Date()
@@ -331,9 +341,10 @@ final class BaseAPSManager: APSManager, Injectable {
                 debug(.apsManager, "\(DebuggingIdentifiers.failed) Failed to complete Loop: \(error)")
                 debug(.apsManager, "\(DebuggingIdentifiers.failed) Failed to complete Loop: \(error)")
             }
             }
 
 
-            // End the background task on the async path itself
-            if bgTask != .invalid {
-                await UIApplication.shared.endBackgroundTask(bgTask)
+            // End the background task
+            if taskID != .invalid {
+                await UIApplication.shared.endBackgroundTask(taskID)
+                taskID = .invalid
             }
             }
         }
         }
     }
     }
@@ -341,15 +352,15 @@ final class BaseAPSManager: APSManager, Injectable {
     private func executeLoop(loopStatRecord: inout LoopStats) async throws {
     private func executeLoop(loopStatRecord: inout LoopStats) async throws {
         try await determineBasal()
         try await determineBasal()
 
 
-        let endDate = Date()
-        loopStatRecord.end = endDate
-        loopStatRecord.duration = roundDouble((endDate - loopStatRecord.start).timeInterval / 60, 2)
-        loopStatRecord.loopStatus = "Success"
-
         // Closed loop: also enact the determination.
         // Closed loop: also enact the determination.
         if settings.closedLoop {
         if settings.closedLoop {
             try await enactDetermination()
             try await enactDetermination()
         }
         }
+
+        let endDate = Date()
+        loopStatRecord.end = endDate
+        loopStatRecord.duration = roundDouble((endDate - loopStatRecord.start).timeInterval / 60, 2)
+        loopStatRecord.loopStatus = "Success"
     }
     }
 
 
     private func calculateLoopInterval(loopStartDate: Date) async -> Double? {
     private func calculateLoopInterval(loopStartDate: Date) async -> Double? {
@@ -491,14 +502,13 @@ final class BaseAPSManager: APSManager, Injectable {
         do {
         do {
             let now = Date()
             let now = Date()
 
 
-            // Parallelize the fetches using async let
-            async let currentTemp = fetchCurrentTempBasal(date: now)
-            async let autosenseResult = autosense()
-
-            _ = try await autosenseResult
+            // put profile creation up front since autosens needs it
             try await openAPS.createProfiles(useSwiftOref: settings.useSwiftOref)
             try await openAPS.createProfiles(useSwiftOref: settings.useSwiftOref)
+            let currentTemp = try await fetchCurrentTempBasal(date: now)
+            _ = try await autosense()
+
             let determination = try await openAPS.determineBasal(
             let determination = try await openAPS.determineBasal(
-                currentTemp: await currentTemp,
+                currentTemp: currentTemp,
                 shouldSmoothGlucose: settingsManager.settings.smoothGlucose,
                 shouldSmoothGlucose: settingsManager.settings.smoothGlucose,
                 useSwiftOref: settings.useSwiftOref,
                 useSwiftOref: settings.useSwiftOref,
                 clock: now
                 clock: now