Skip to content

Instantly share code, notes, and snippets.

@tshortli
Last active January 31, 2022 08:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tshortli/e6f0dc0405a61884c99147576de96c9b to your computer and use it in GitHub Desktop.
Save tshortli/e6f0dc0405a61884c99147576de96c9b to your computer and use it in GitHub Desktop.
A Swift async function that gets the most recent workouts from HealthKit
func mostRecentWorkouts(for healthStore: HKHealthStore, limit: Int) async throws -> [HKWorkout] {
let query = HKSampleQueryDescriptor(
predicates: [.workout()],
sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)],
limit: limit
)
return try await query.result(for: healthStore)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment