Browse Source

Merge pull request #324 from nightscout/hotfix-g7-crashes

marv-out 1 year ago
parent
commit
182d967d9d

+ 4 - 3
Trio/Sources/APS/CGM/PluginSource.swift

@@ -7,7 +7,7 @@ import LoopKit
 import LoopKitUI
 
 final class PluginSource: GlucoseSource {
-    private let processQueue = DispatchQueue(label: "DexcomSource.processQueue")
+    private let processQueue = DispatchQueue(label: "CGMPluginSource.processQueue")
     private let glucoseStorage: GlucoseStorage!
     var glucoseManager: FetchGlucoseManager?
 
@@ -159,8 +159,9 @@ extension PluginSource: CGMManagerDelegate {
         UUID().uuidString
     }
 
-    func cgmManager(_: CGMManager, didUpdate status: CGMManagerStatus) {
-        debug(.deviceManager, "DEBUG DID UPDATE STATE")
+    func cgmManager(_ cgmManager: CGMManager, didUpdate status: CGMManagerStatus) {
+        debug(.deviceManager, "CGM Manager did update state to \(status)")
+        
         processQueue.async {
             if self.cgmHasValidSensorSession != status.hasValidSensorSession {
                 self.cgmHasValidSensorSession = status.hasValidSensorSession

+ 27 - 5
Trio/Sources/APS/FetchGlucoseManager.swift

@@ -126,11 +126,15 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
         // if plugin, if not the same pluginID, need to reset the cgmManager
         // if plugin and newManager provides, update cgmManager
         debug(.apsManager, "plugin : \(String(describing: cgmManager?.pluginIdentifier))")
-        if let manager = newManager
-        {
-            cgmManager = manager
-            glucoseSource = nil
-            removeCalibrations()
+
+        if let manager = newManager {
+            // If the pointer to manager is the *same* as our current `cgmManager`, skip re-init
+            if manager !== cgmManager {
+                // or do a more thorough check to see if it is the same class & state
+                removeCalibrations()
+                cgmManager = manager
+                glucoseSource = nil
+            }
         } else if self.cgmGlucoseSourceType == .plugin, cgmManager == nil, let rawCGMManager = rawCGMManager {
             cgmManager = cgmManagerFromRawValue(rawCGMManager)
             updateManagerUnits(cgmManager)
@@ -339,6 +343,24 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
     }
 }
 
+extension FetchGlucoseManager {
+    /// Dispatches given `functionToInvoke` to the CGM manager's queue (if any).
+    func performOnCGMManagerQueue(_ functionToInvoke: @escaping () -> Void) {
+        // If a CGM manager exists and it defines a delegate queue, use it
+        if let cgmManager = self.cgmManager,
+           let managerQueue = cgmManager.delegateQueue
+        {
+            managerQueue.async {
+                functionToInvoke()
+            }
+        } else {
+            // If there's no cgmManager or no queue, just run the block immediately
+            // This possibly executes `functionToInvoke` on main thread
+            functionToInvoke()
+        }
+    }
+}
+
 extension CGMManager {
     typealias RawValue = [String: Any]
 

+ 4 - 4
Trio/Sources/Application/TrioApp.swift

@@ -67,20 +67,20 @@ import Swinject
             .default,
             "Trio Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(String(describing: BuildDetails.default.buildDate()))] [buildExpires: \(String(describing: BuildDetails.default.calculateExpirationDate()))]"
         )
-        
+
         // Setup up the Core Data Stack
         coreDataStack = CoreDataStack.shared
 
         do {
             // Explicitly initialize Core Data Stacak
             try coreDataStack.initializeStack()
-            
+
             // Load services
             loadServices()
-            
+
             // Fix bug in iOS 18 related to the translucent tab bar
             configureTabBarAppearance()
-            
+
             // Clear the persistentHistory and the NSManagedObjects that are older than 90 days every time the app starts
             cleanupOldData()
         } catch {

+ 2 - 2
Trio/Sources/Logger/Logger.swift

@@ -157,13 +157,13 @@ final class Logger {
             case .apsManager,
                  .bolusState,
                  .businessLogic,
+                 .coreData,
                  .deviceManager,
                  .nightscout,
                  .openAPS,
                  .remoteControl,
                  .service,
-                 .watchManager,
-                 .coreData:
+                 .watchManager:
                 return OSLog(subsystem: subsystem, category: name)
             }
         }

+ 10 - 6
Trio/Sources/Modules/CGMSettings/CGMSettingsStateModel.swift

@@ -134,12 +134,16 @@ extension CGMSettings {
         }
 
         func deleteCGM() {
-            shouldDisplayCGMSetupSheet = false
-
-            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
-                self.fetchGlucoseManager.deleteGlucoseSource()
-                self.completionNotifyingDidComplete(OtherCGMSourceCompletionNotifying())
-            })
+            fetchGlucoseManager.performOnCGMManagerQueue {
+                // Call plugin functionality on the manager queue (or at least attempt to)
+                self.fetchGlucoseManager?.deleteGlucoseSource()
+
+                // UI updates go back to Main
+                DispatchQueue.main.async {
+                    self.shouldDisplayCGMSetupSheet = false
+                    self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
+                }
+            }
         }
     }
 }

+ 9 - 5
Trio/Sources/Modules/Home/HomeStateModel.swift

@@ -454,12 +454,16 @@ extension Home {
         }
 
         func deleteCGM() {
-            shouldDisplayCGMSetupSheet = false
-
-            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
+            fetchGlucoseManager.performOnCGMManagerQueue {
+                // Call plugin functionality on the manager queue (or at least attempt to)
                 self.fetchGlucoseManager?.deleteGlucoseSource()
-                self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
-            })
+
+                // UI updates go back to Main
+                DispatchQueue.main.async {
+                    self.shouldDisplayCGMSetupSheet = false
+                    self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
+                }
+            }
         }
 
         /// Display the eventual status message provided by the manager of the pump