@@ -69,6 +69,9 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
6969 . receive ( on: DispatchQueue . global ( qos: . background) )
7070 . sink { [ weak self] _ in
7171 guard let self = self else { return }
72+ // Skip if no watch is paired or app not installed
73+ guard let session = self . session, session. isPaired, session. isReachable,
74+ session. isWatchAppInstalled else { return }
7275 Task {
7376 let state = await self . setupWatchState ( )
7477 await self . sendDataToWatch ( state)
@@ -82,6 +85,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
8285 private func registerHandlers( ) {
8386 coreDataPublisher? . filteredByEntityName ( " OrefDetermination " ) . sink { [ weak self] _ in
8487 guard let self = self else { return }
88+ // Skip if no watch is paired or app not installed
89+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
8590 Task {
8691 let state = await self . setupWatchState ( )
8792 await self . sendDataToWatch ( state)
@@ -91,6 +96,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
9196 // Due to the Batch insert this only is used for observing Deletion of Glucose entries
9297 coreDataPublisher? . filteredByEntityName ( " GlucoseStored " ) . sink { [ weak self] _ in
9398 guard let self = self else { return }
99+ // Skip if no watch is paired or app not installed
100+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
94101 Task {
95102 let state = await self . setupWatchState ( )
96103 await self . sendDataToWatch ( state)
@@ -106,6 +113,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
106113
107114 coreDataPublisher? . filteredByEntityName ( " OverrideStored " ) . sink { [ weak self] _ in
108115 guard let self = self else { return }
116+ // Skip if no watch is paired or app not installed
117+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
109118 Task {
110119 let state = await self . setupWatchState ( )
111120 await self . sendDataToWatch ( state)
@@ -114,6 +123,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
114123
115124 coreDataPublisher? . filteredByEntityName ( " TempTargetStored " ) . sink { [ weak self] _ in
116125 guard let self = self else { return }
126+ // Skip if no watch is paired or app not installed
127+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
117128 Task {
118129 let state = await self . setupWatchState ( )
119130 await self . sendDataToWatch ( state)
@@ -148,6 +159,17 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
148159 /// Prepares the current state data to be sent to the Watch
149160 /// - Returns: WatchState containing current glucose readings and trends and determination infos for displaying cob and iob in the view
150161 func setupWatchState( ) async -> WatchState {
162+ // Check if a watch is paired and reachable before doing expensive calculations
163+ guard let session = session, session. isPaired, session. isReachable, session. isWatchAppInstalled else {
164+ debug ( . watchManager, " ⌚️❌ Skipping setupWatchState - No Watch is paired or app not installed " )
165+ return WatchState ( date: Date ( ) )
166+ }
167+
168+ // Skip if watch session is not activated
169+ guard session. activationState == . activated else {
170+ debug ( . watchManager, " ⌚️❌ Skipping setupWatchState - Watch session not activated " )
171+ return WatchState ( date: Date ( ) )
172+ }
151173 do {
152174 // Get NSManagedObjectIDs
153175 let glucoseIds = try await fetchGlucose ( )
@@ -527,7 +549,9 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
527549 {
528550 debug ( . watchManager, " 📱 Watch requested watch state data update. " )
529551 guard let self = self else { return }
530-
552+ // Skip if no watch is paired or app not installed
553+ guard let session = self . session, session. isPaired, session. isReachable,
554+ session. isWatchAppInstalled else { return }
531555 Task {
532556 let state = await self . setupWatchState ( )
533557 await self . sendDataToWatch ( state)
@@ -1137,6 +1161,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
11371161extension BaseWatchManager: SettingsObserver, PumpSettingsObserver {
11381162 // to update maxBolus
11391163 func pumpSettingsDidChange( _: PumpSettings ) {
1164+ // Skip if no watch is paired or app not installed
1165+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
11401166 Task {
11411167 let state = await self . setupWatchState ( )
11421168 await self . sendDataToWatch ( state)
@@ -1150,6 +1176,9 @@ extension BaseWatchManager: SettingsObserver, PumpSettingsObserver {
11501176 lowGlucose = settingsManager. settings. low
11511177 highGlucose = settingsManager. settings. high
11521178
1179+ // Skip if no watch is paired or app not installed
1180+ guard let session = self . session, session. isPaired, session. isReachable, session. isWatchAppInstalled else { return }
1181+
11531182 Task {
11541183 let state = await self . setupWatchState ( )
11551184 await self . sendDataToWatch ( state)
0 commit comments