From d38c87929753e8c217f68b6cac8ab90e49050c0f Mon Sep 17 00:00:00 2001 From: Matthew Turk Date: Mon, 26 Feb 2024 10:57:46 -0800 Subject: [PATCH 1/4] More specific error handling --- PAWS/PAWSStandard.swift | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/PAWS/PAWSStandard.swift b/PAWS/PAWSStandard.swift index c6d71bd6..86cd2eea 100644 --- a/PAWS/PAWSStandard.swift +++ b/PAWS/PAWSStandard.swift @@ -81,25 +81,39 @@ actor PAWSStandard: Standard, EnvironmentAccessible, HealthKitConstraint, Onboar private func upload(electrocardiogram: HKElectrocardiogram) async { var supplementalMetrics: [HKSample] = [] + var precedingPulseRates: [HKQuantitySample] = [] + var precedingPhysicalEffort: [HKQuantitySample] = [] + var precedingStepCount: [HKQuantitySample] = [] + var precedingActiveEnergy: [HKQuantitySample] = [] + var precedingVo2Max: HKQuantitySample? do { try await upload(sample: electrocardiogram) - - supplementalMetrics.append(contentsOf: try await electrocardiogram.precedingPulseRates) - supplementalMetrics.append(contentsOf: try await electrocardiogram.precedingPhysicalEffort) - supplementalMetrics.append(contentsOf: try await electrocardiogram.precedingStepCount) - supplementalMetrics.append(contentsOf: try await electrocardiogram.precedingActiveEnergy) - - if let precedingVo2Max = try await electrocardiogram.precedingVo2Max { - supplementalMetrics.append(precedingVo2Max) - } - - for supplementalMetric in supplementalMetrics { - try await upload(sample: supplementalMetric) - } + precedingPulseRates = try await electrocardiogram.precedingPulseRates + precedingPhysicalEffort = try await electrocardiogram.precedingPhysicalEffort + precedingStepCount = try await electrocardiogram.precedingStepCount + precedingActiveEnergy = try await electrocardiogram.precedingActiveEnergy + precedingVo2Max = try await electrocardiogram.precedingVo2Max } catch { logger.log("Could not access HealthKit sample: \(error)") } + + supplementalMetrics.append(contentsOf: precedingPulseRates) + supplementalMetrics.append(contentsOf: precedingPhysicalEffort) + supplementalMetrics.append(contentsOf: precedingStepCount) + supplementalMetrics.append(contentsOf: precedingActiveEnergy) + + if let precedingVo2Max { + supplementalMetrics.append(precedingVo2Max) + } + + for supplementalMetric in supplementalMetrics { + do { + try await upload(sample: supplementalMetric) + } catch { + logger.log("Could not upload \(supplementalMetric.sampleType): \(error)") + } + } } private func updateElectrocardiogram(basedOn categorySample: HKCategorySample) async { From c3e53c3c8c813751253d0f8c3d3faa7da9afd550 Mon Sep 17 00:00:00 2001 From: Matthew Turk Date: Mon, 26 Feb 2024 15:48:05 -0800 Subject: [PATCH 2/4] Append samples with `try?` --- PAWS/PAWSStandard.swift | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/PAWS/PAWSStandard.swift b/PAWS/PAWSStandard.swift index 86cd2eea..3fe466a6 100644 --- a/PAWS/PAWSStandard.swift +++ b/PAWS/PAWSStandard.swift @@ -81,38 +81,24 @@ actor PAWSStandard: Standard, EnvironmentAccessible, HealthKitConstraint, Onboar private func upload(electrocardiogram: HKElectrocardiogram) async { var supplementalMetrics: [HKSample] = [] - var precedingPulseRates: [HKQuantitySample] = [] - var precedingPhysicalEffort: [HKQuantitySample] = [] - var precedingStepCount: [HKQuantitySample] = [] - var precedingActiveEnergy: [HKQuantitySample] = [] - var precedingVo2Max: HKQuantitySample? do { try await upload(sample: electrocardiogram) - precedingPulseRates = try await electrocardiogram.precedingPulseRates - precedingPhysicalEffort = try await electrocardiogram.precedingPhysicalEffort - precedingStepCount = try await electrocardiogram.precedingStepCount - precedingActiveEnergy = try await electrocardiogram.precedingActiveEnergy - precedingVo2Max = try await electrocardiogram.precedingVo2Max - } catch { - logger.log("Could not access HealthKit sample: \(error)") - } - - supplementalMetrics.append(contentsOf: precedingPulseRates) - supplementalMetrics.append(contentsOf: precedingPhysicalEffort) - supplementalMetrics.append(contentsOf: precedingStepCount) - supplementalMetrics.append(contentsOf: precedingActiveEnergy) - - if let precedingVo2Max { - supplementalMetrics.append(precedingVo2Max) - } - - for supplementalMetric in supplementalMetrics { - do { + + supplementalMetrics.append(contentsOf: (try? await electrocardiogram.precedingPulseRates) ?? []) + supplementalMetrics.append(contentsOf: (try? await electrocardiogram.precedingPhysicalEffort) ?? []) + supplementalMetrics.append(contentsOf: (try? await electrocardiogram.precedingStepCount) ?? []) + supplementalMetrics.append(contentsOf: (try? await electrocardiogram.precedingActiveEnergy) ?? []) + + if let precedingVo2Max = try? await electrocardiogram.precedingVo2Max { + supplementalMetrics.append(precedingVo2Max) + } + + for supplementalMetric in supplementalMetrics { try await upload(sample: supplementalMetric) - } catch { - logger.log("Could not upload \(supplementalMetric.sampleType): \(error)") } + } catch { + logger.log("Could not access HealthKit sample: \(error)") } } From b269cdcb4b964e8877373394eda558efa5df843a Mon Sep 17 00:00:00 2001 From: Matthew Turk Date: Mon, 26 Feb 2024 15:49:54 -0800 Subject: [PATCH 3/4] One more `try?` --- PAWS/PAWSStandard.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PAWS/PAWSStandard.swift b/PAWS/PAWSStandard.swift index 3fe466a6..d7086abd 100644 --- a/PAWS/PAWSStandard.swift +++ b/PAWS/PAWSStandard.swift @@ -95,7 +95,7 @@ actor PAWSStandard: Standard, EnvironmentAccessible, HealthKitConstraint, Onboar } for supplementalMetric in supplementalMetrics { - try await upload(sample: supplementalMetric) + try? await upload(sample: supplementalMetric) } } catch { logger.log("Could not access HealthKit sample: \(error)") From f2716906b5fa18085886cee1bd4e82a9ddf4cf57 Mon Sep 17 00:00:00 2001 From: Matthew Turk Date: Tue, 27 Feb 2024 09:33:25 -0800 Subject: [PATCH 4/4] Add log statements for individual uploads --- PAWS/PAWSStandard.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PAWS/PAWSStandard.swift b/PAWS/PAWSStandard.swift index d7086abd..a8fd3da3 100644 --- a/PAWS/PAWSStandard.swift +++ b/PAWS/PAWSStandard.swift @@ -95,7 +95,11 @@ actor PAWSStandard: Standard, EnvironmentAccessible, HealthKitConstraint, Onboar } for supplementalMetric in supplementalMetrics { - try? await upload(sample: supplementalMetric) + do { + try await upload(sample: supplementalMetric) + } catch { + logger.log("Could not upload \(supplementalMetric.sampleType): \(error)") + } } } catch { logger.log("Could not access HealthKit sample: \(error)")