@@ -303,16 +303,14 @@ class HealthDataReader {
303303 }
304304 } else {
305305 if #available( iOS 14 . 0 , * ) , let ecgSamples = samples as? [ HKElectrocardiogram ] {
306- let dictionaries = ecgSamples. map ( self . fetchEcgMeasurements)
307- DispatchQueue . main. async {
308- result ( dictionaries)
309- }
306+ self . fetchEcgMeasurements ( ecgSamples, result)
310307 } else {
311308 DispatchQueue . main. async {
312309 print ( " Error getting ECG - only available on iOS 14.0 and above! " )
313310 result ( nil )
314311 }
315312 }
313+
316314 }
317315 }
318316
@@ -546,38 +544,62 @@ class HealthDataReader {
546544 /// - Parameter sample: ECG sample
547545 /// - Returns: Dictionary with ECG data
548546 @available ( iOS 14 . 0 , * )
549- private func fetchEcgMeasurements( _ sample: HKElectrocardiogram ) -> NSDictionary {
550- let semaphore = DispatchSemaphore ( value: 0 )
551- var voltageValues = [ NSDictionary] ( )
552- let voltageQuery = HKElectrocardiogramQuery ( sample) { query, result in
553- switch result {
554- case let . measurement( measurement) :
555- if let voltageQuantity = measurement. quantity ( for: . appleWatchSimilarToLeadI) {
556- let voltage = voltageQuantity. doubleValue ( for: HKUnit . volt ( ) )
557- let timeSinceSampleStart = measurement. timeSinceSampleStart
558- voltageValues. append ( [ " voltage " : voltage, " timeSinceSampleStart " : timeSinceSampleStart] )
547+ private func fetchEcgMeasurements( _ ecgSample: [ HKElectrocardiogram ] , _ result: @escaping FlutterResult ) {
548+ let group = DispatchGroup ( )
549+ var dictionaries = [ NSDictionary] ( )
550+ let lock = NSLock ( )
551+
552+ for ecg in ecgSample {
553+ group. enter ( )
554+
555+ var voltageValues = [ [ String: Any] ] ( )
556+ let expected = Int ( ecg. numberOfVoltageMeasurements)
557+ if expected > 0 {
558+ voltageValues. reserveCapacity ( expected)
559+ }
560+
561+ let q = HKElectrocardiogramQuery ( ecg) { _, res in
562+ switch res {
563+ case . measurement( let m) :
564+ if let v = m. quantity ( for: . appleWatchSimilarToLeadI) ?
565+ . doubleValue ( for: HKUnit . volt ( ) ) {
566+ voltageValues. append ( [
567+ " voltage " : v,
568+ " timeSinceSampleStart " : m. timeSinceSampleStart
569+ ] )
570+ }
571+ case . done:
572+ let dict : NSDictionary = [
573+ " uuid " : " \( ecg. uuid) " ,
574+ " voltageValues " : voltageValues,
575+ " averageHeartRate " : ecg. averageHeartRate?
576+ . doubleValue ( for: HKUnit . count ( )
577+ . unitDivided ( by: HKUnit . minute ( ) ) ) ,
578+ " samplingFrequency " : ecg. samplingFrequency?
579+ . doubleValue ( for: HKUnit . hertz ( ) ) ,
580+ " classification " : ecg. classification. rawValue,
581+ " date_from " : Int ( ecg. startDate. timeIntervalSince1970 * 1000 ) ,
582+ " date_to " : Int ( ecg. endDate. timeIntervalSince1970 * 1000 ) ,
583+ " source_id " : ecg. sourceRevision. source. bundleIdentifier,
584+ " source_name " : ecg. sourceRevision. source. name
585+ ]
586+ lock. lock ( )
587+ dictionaries. append ( dict)
588+ lock. unlock ( )
589+ group. leave ( )
590+ case . error( let e) :
591+ print ( " ECG query error: \( e) " )
592+ group. leave ( )
593+ @unknown default :
594+ print ( " ECG query unknown result " )
595+ group. leave ( )
559596 }
560- case . done:
561- semaphore. signal ( )
562- case let . error( error) :
563- print ( error)
564- @unknown default :
565- print ( " Unknown error occurred " )
566597 }
598+ self . healthStore. execute ( q)
599+ }
600+
601+ group. notify ( queue: . main) {
602+ result ( dictionaries)
567603 }
568- healthStore. execute ( voltageQuery)
569- semaphore. wait ( )
570- return [
571- " uuid " : " \( sample. uuid) " ,
572- " voltageValues " : voltageValues,
573- " averageHeartRate " : sample. averageHeartRate? . doubleValue (
574- for: HKUnit . count ( ) . unitDivided ( by: HKUnit . minute ( ) ) ) ,
575- " samplingFrequency " : sample. samplingFrequency? . doubleValue ( for: HKUnit . hertz ( ) ) ,
576- " classification " : sample. classification. rawValue,
577- " date_from " : Int ( sample. startDate. timeIntervalSince1970 * 1000 ) ,
578- " date_to " : Int ( sample. endDate. timeIntervalSince1970 * 1000 ) ,
579- " source_id " : sample. sourceRevision. source. bundleIdentifier,
580- " source_name " : sample. sourceRevision. source. name,
581- ]
582604 }
583605}
0 commit comments