@@ -59,18 +59,20 @@ async throws -> [HKQuantityType: HKUnit] {
5959 return try await withCheckedThrowingContinuation { continuation in
6060 store. preferredUnits ( for: Set ( quantityTypes) ) {
6161 ( typePerUnits: [ HKQuantityType : HKUnit ] , error: Error ? ) in
62- if let error = error {
63- return continuation. resume ( throwing: error)
64- }
62+ DispatchQueue . main. async {
63+ if let error = error {
64+ return continuation. resume ( throwing: error)
65+ }
6566
66- // Thread-safe write: barrier ensures exclusive access
67- quantityTypeCacheQueue. sync ( flags: . barrier) {
68- typePerUnits. forEach { ( type: HKQuantityType , unit: HKUnit ) in
69- quantityTypeUnitCache. updateValue ( unit, forKey: type)
67+ // Thread-safe write: barrier ensures exclusive access
68+ quantityTypeCacheQueue. sync ( flags: . barrier) {
69+ typePerUnits. forEach { ( type: HKQuantityType , unit: HKUnit ) in
70+ quantityTypeUnitCache. updateValue ( unit, forKey: type)
71+ }
7072 }
71- }
7273
73- return continuation. resume ( returning: typePerUnits)
74+ return continuation. resume ( returning: typePerUnits)
75+ }
7476 }
7577 }
7678}
@@ -157,17 +159,18 @@ class CoreModule: HybridCoreModuleSpec {
157159 }
158160 return store. getRequestStatusForAuthorization ( toShare: toShare, read: toRead) {
159161 status, error in
160- if let error = error {
161- continuation. resume ( throwing: error)
162- } else {
163- if let authStatus = AuthorizationRequestStatus ( rawValue: Int32 ( status. rawValue) ) {
164- continuation. resume ( returning: authStatus)
162+ DispatchQueue . main. async {
163+ if let error = error {
164+ continuation. resume ( throwing: error)
165165 } else {
166- continuation. resume (
167- throwing: runtimeErrorWithPrefix (
168- " Unrecognized authStatus returned: \( status. rawValue) " ) )
166+ if let authStatus = AuthorizationRequestStatus ( rawValue: Int32 ( status. rawValue) ) {
167+ continuation. resume ( returning: authStatus)
168+ } else {
169+ continuation. resume (
170+ throwing: runtimeErrorWithPrefix (
171+ " Unrecognized authStatus returned: \( status. rawValue) " ) )
172+ }
169173 }
170-
171174 }
172175 }
173176 }
@@ -181,10 +184,12 @@ class CoreModule: HybridCoreModuleSpec {
181184
182185 return try await withCheckedThrowingContinuation { continuation in
183186 store. requestAuthorization ( toShare: share, read: toRead) { status, error in
184- if let error = error {
185- continuation. resume ( throwing: error)
186- } else {
187- continuation. resume ( returning: status)
187+ DispatchQueue . main. async {
188+ if let error = error {
189+ continuation. resume ( throwing: error)
190+ } else {
191+ continuation. resume ( returning: status)
192+ }
188193 }
189194 }
190195 }
@@ -203,25 +208,27 @@ class CoreModule: HybridCoreModuleSpec {
203208 sampleType: sampleType,
204209 samplePredicate: predicate
205210 ) { ( _: HKSourceQuery , sources: Set < HKSource > ? , error: Error ? ) in
206- if let error = error {
207- continuation. resume ( throwing: error)
208- return
209- }
211+ DispatchQueue . main. async {
212+ if let error = error {
213+ continuation. resume ( throwing: error)
214+ return
215+ }
210216
211- guard let sources = sources else {
212- return continuation. resume (
213- throwing: runtimeErrorWithPrefix (
214- " Empty response for sample type \( identifier. stringValue) " ) )
215- }
217+ guard let sources = sources else {
218+ return continuation. resume (
219+ throwing: runtimeErrorWithPrefix (
220+ " Empty response for sample type \( identifier. stringValue) " ) )
221+ }
216222
217- let serializedSources = sources. map { source -> SourceProxy in
223+ let serializedSources = sources. map { source -> SourceProxy in
218224
219- return SourceProxy (
220- source: source
221- )
222- }
225+ return SourceProxy (
226+ source: source
227+ )
228+ }
223229
224- continuation. resume ( returning: serializedSources)
230+ continuation. resume ( returning: serializedSources)
231+ }
225232 }
226233
227234 store. execute ( query)
@@ -240,10 +247,12 @@ class CoreModule: HybridCoreModuleSpec {
240247 for: type,
241248 frequency: frequency
242249 ) { ( success, error) in
243- if let err = error {
244- return continuation. resume ( throwing: err)
250+ DispatchQueue . main. async {
251+ if let err = error {
252+ return continuation. resume ( throwing: err)
253+ }
254+ return continuation. resume ( returning: success)
245255 }
246- return continuation. resume ( returning: success)
247256 }
248257 }
249258 } else {
@@ -262,10 +271,12 @@ class CoreModule: HybridCoreModuleSpec {
262271 store. disableBackgroundDelivery (
263272 for: type
264273 ) { ( success, error) in
265- if let err = error {
266- return continuation. resume ( throwing: err)
274+ DispatchQueue . main. async {
275+ if let err = error {
276+ return continuation. resume ( throwing: err)
277+ }
278+ return continuation. resume ( returning: success)
267279 }
268- return continuation. resume ( returning: success)
269280 }
270281 }
271282 }
@@ -275,10 +286,12 @@ class CoreModule: HybridCoreModuleSpec {
275286 return Promise . async {
276287 try await withCheckedThrowingContinuation { continuation in
277288 store. disableAllBackgroundDelivery ( completion: { ( success, error) in
278- guard let err = error else {
279- return continuation. resume ( returning: success)
289+ DispatchQueue . main. async {
290+ guard let err = error else {
291+ return continuation. resume ( returning: success)
292+ }
293+ return continuation. resume ( throwing: err)
280294 }
281- return continuation. resume ( throwing: err)
282295 } )
283296 }
284297 }
@@ -345,10 +358,12 @@ class CoreModule: HybridCoreModuleSpec {
345358 let of = try sampleTypeFrom ( sampleTypeIdentifierWriteable: objectTypeIdentifier)
346359 return try await withCheckedThrowingContinuation { continuation in
347360 store. deleteObjects ( of: of, predicate: predicate) { ( _, count, error) in
348- if let error = error {
349- continuation. resume ( throwing: error)
350- } else {
351- continuation. resume ( returning: Double ( count) )
361+ DispatchQueue . main. async {
362+ if let error = error {
363+ continuation. resume ( throwing: error)
364+ } else {
365+ continuation. resume ( returning: Double ( count) )
366+ }
352367 }
353368 }
354369 }
0 commit comments