Procházet zdrojové kódy

Fix dispatch queues and CD publisher

Deniz Cengiz před 1 rokem
rodič
revize
a2fa7f3fec

+ 6 - 7
Trio Watch App Extension/WatchState.swift

@@ -334,7 +334,7 @@ import WatchConnectivity
             }
         }
     }
-    
+
     /// Conditionally triggers a watch state update if the last known update was too long ago or has never occurred.
     ///
     /// This method checks the `lastWatchStateUpdate` timestamp to determine how many seconds
@@ -344,10 +344,10 @@ import WatchConnectivity
     ///
     /// it will show a syncing animation and request a new watch state update from the iPhone app.
     private func forceConditionalWatchStateUpdate() {
-        guard let lastUpdateTimestamp = self.lastWatchStateUpdate else {
+        guard let lastUpdateTimestamp = lastWatchStateUpdate else {
             // If there's no recorded timestamp, we must force a fresh update immediately.
-            self.showSyncingAnimation = true
-            self.requestWatchStateUpdate()
+            showSyncingAnimation = true
+            requestWatchStateUpdate()
             return
         }
 
@@ -356,13 +356,12 @@ import WatchConnectivity
 
         // If more than 15 seconds have elapsed since the last update, force an(other) update.
         if secondsSinceUpdate > 15 {
-            self.showSyncingAnimation = true
-            self.requestWatchStateUpdate()
+            showSyncingAnimation = true
+            requestWatchStateUpdate()
             return
         }
     }
 
-
     /// Handles incoming messages that either contain an acknowledgement or fresh watchState data  (<15 min)
     private func processWatchMessage(_ message: [String: Any]) {
         DispatchQueue.main.async {

+ 1 - 1
Trio/Sources/Modules/Home/View/Header/PumpView.swift

@@ -101,7 +101,7 @@ struct PumpView: View {
                         Image(systemName: "stopwatch.fill")
                             .font(.callout)
                             .foregroundStyle(timerColor)
-                        
+
                         let remainingTimeString = remainingTimeString(time: date.timeIntervalSince(timerDate))
 
                         Text(remainingTimeString)

+ 4 - 2
Trio/Sources/Services/WatchManager/AppleWatchManager.swift

@@ -33,7 +33,9 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
     private var currentGlucoseTarget: Decimal = 100.0
     private var activeBolusAmount: Double = 0.0
 
-    private var coreDataPublisher: AnyPublisher<Set<NSManagedObject>, Never>?
+    // Queue for handling Core Data change notifications
+    private let queue = DispatchQueue(label: "BaseWatchManagerManager.queue", qos: .utility)
+    private var coreDataPublisher: AnyPublisher<Set<NSManagedObjectID>, Never>?
     private var subscriptions = Set<AnyCancellable>()
 
     typealias PumpEvent = PumpEventStored.EventType
@@ -60,7 +62,7 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
         // Observer for OrefDetermination and adjustments
         coreDataPublisher =
             changedObjectsOnManagedObjectContextDidSavePublisher()
-                .receive(on: DispatchQueue.global(qos: .background))
+                .receive(on: queue)
                 .share()
                 .eraseToAnyPublisher()
 

+ 5 - 2
Trio/Sources/Services/WatchManager/GarminManager.swift

@@ -88,8 +88,11 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
     /// Current glucose units, either mg/dL or mmol/L, read from user settings.
     private var units: GlucoseUnits = .mgdL
 
+    /// Queue for handling Core Data change notifications
+    private let queue = DispatchQueue(label: "BaseGarminManager.queue", qos: .utility)
+
     /// Publishes any changed CoreData objects that match our filters (e.g., OrefDetermination, GlucoseStored).
-    private var coreDataPublisher: AnyPublisher<Set<NSManagedObject>, Never>?
+    private var coreDataPublisher: AnyPublisher<Set<NSManagedObjectID>, Never>?
 
     /// Additional local subscriptions (separate from `cancellables`) for CoreData events.
     private var subscriptions = Set<AnyCancellable>()
@@ -122,7 +125,7 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
 
         coreDataPublisher =
             changedObjectsOnManagedObjectContextDidSavePublisher()
-                .receive(on: DispatchQueue.global(qos: .background))
+                .receive(on: queue)
                 .share()
                 .eraseToAnyPublisher()