My goal right now is to design a query that will fetch distances for each sport I have in my requests set by month and return an object with the organized data. I’m blocked on how to set it up in a way where it loops ever each sport to do a request. If that’s even possible in this case.
HealthKitModel
import Foundation
struct DistancesByYear {
let year: Int
let rowing: DistancePerMonth
let cycling: DistancePerMonth
let swimming: DistancePerMonth
let paddleSports: DistancePerMonth
let skatingSports: DistancePerMonth
let walkingRunning: DistancePerMonth
let crossCountrySkiing: DistancePerMonth
let downhillSnowSports: DistancePerMonth
}
struct DistancePerMonth {
let january: Int
let february: Int
let march: Int
let april: Int
let may: Int
let june: Int
let july: Int
let august: Int
let september: Int
let october: Int
let november: Int
let december: Int
}
HealthKitOO
import Observation
import HealthKit
@Observable final class HealthKitOO {
private let healthStore: HKHealthStore = HKHealthStore()
private let shareRequests: Set<HKSampleType> = []
private let readRequests: Set<HKSampleType> = [
HKQuantityType(.distanceRowing),
HKQuantityType(.distanceCycling),
HKQuantityType(.distanceSwimming),
HKQuantityType(.distancePaddleSports),
HKQuantityType(.distanceSkatingSports),
HKQuantityType(.distanceWalkingRunning),
HKQuantityType(.distanceCrossCountrySkiing),
HKQuantityType(.distanceDownhillSnowSports),
]
func requestHealthData() async {
do {
try await healthStore.requestAuthorization(toShare: shareRequests, read: readRequests)
} catch {
print(error.localizedDescription)
}
}
func sortDistancesByYear(year: Int) async -> DistancesByYear {
}
}