HKSampleQueryDescriptor
A query interface that reads samples using Swift concurrency.
Declaration
struct HKSampleQueryDescriptor<Sample> where Sample : HKSampleMentioned in
Overview
Use HKSampleQueryDescriptor to run a general query that returns a snapshot of all the matching samples currently saved in the HealthKit store.
// Define the type.
let stepType = HKQuantityType(.stepCount)
// Create the descriptor.
let descriptor = HKSampleQueryDescriptor(
predicates:[.quantitySample(type: stepType)],
sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)],
limit: 10)
// Launch the query and wait for the results.
// The system automatically sets results to [HKQuantitySample].
let results = try await descriptor.result(for: store)
for result in results {
// Process the results here.
}When you call the descriptor’s result(for:) method, it creates and executes an HKSampleQuery in the background, passing the results from the query’s resultsHandler as its return value.