HKQuantitySeriesSampleQueryDescriptor
A query interface that reads the series data associated with quantity samples using Swift concurrency.
Declaration
struct HKQuantitySeriesSampleQueryDescriptorOverview
Use HKQuantitySeriesSampleQueryDescriptor to read the series data included in quantity samples. Apps can save any quantity data as a series using the HKQuantitySeriesSampleBuilder class; however, series data typically comes from high-frequency data saved during a workout. Any HKQuantitySample that has a count greater than 1 contains series data.
To read the individual data entries, create a predicate that identifies the sample type and limits the results to the desired data. For example, to read the series for a particular sample object, include a predicateForObject(with:) predicate. However, if you’re reading high-frequency data during a workout, you may want to include a predicateForSamples(withStart:end:options:) predicate that returns matching series data based on the workout’s time period instead.
// Create the predicate for the data.
let heartRate = HKQuantityType(.heartRate)
let objectPredicate = HKQuery.predicateForObject(with: myHeartRateSample.uuid)
let predicate = HKSamplePredicate.quantitySample(type: heartRate,
predicate: objectPredicate)
// Create the source descriptor.
let seriesDescriptor =
HKQuantitySeriesSampleQueryDescriptor(predicate: predicate,
options: .orderByQuantitySampleStartDate)
// Get the AsyncSequence that returns the individual data entries.
let series = seriesDescriptor.results(for: store)
// Access each data entry in the series
for try await entry in series {
// Process results here.
let steps = entry.quantity.doubleValue(for: .count())
print(steps)
}While this method returns an AsyncSequence, unlike the long-running queries, this sequence has a finite size. Iterating over the sequence asynchronously returns data entries, automatically terminating after you receive all the data.
Topics
Creating Series Query Descriptors
Running Queries
results(for:)HKQuantitySeriesSampleQueryDescriptor.ResultsHKQuantitySeriesSampleQueryDescriptor.Result