Contents

HKStatisticsOptions

Options for specifying the statistic to calculate.

Declaration

struct HKStatisticsOptions

Overview

You cannot combine a discrete option with a cumulative option. You can, however, combine multiple discrete options together to perform multiple calculations. You can also combine the separateBySource option with any of the other options.

Swift

let cumulativeActiveEnergyBurned = HKQuantityType(.activeEnergyBurned)
 
let discreteHeartRate = HKQuantityType(.heartRate)
 
// Cannot combine cumulative options with discrete options.
// However, you can combine a cumulative option and separated by source
let cumulativeQuery = HKStatisticsQuery(quantityType:cumulativeActiveEnergyBurned,
                                        quantitySamplePredicate:nil,
                                        options: [.cumulativeSum, .separateBySource]) {
                                            query, statistics, error in
                                            
                                            // ... process the results here
}
 
// You can also combine any number of discrete options
// and the separated by source option.
let discreteQuery = HKStatisticsQuery(quantityType: discreteHeartRate,
                                      quantitySamplePredicate: nil,
                                      options: [.discreteAverage, .discreteMin, .discreteMax, .separateBySource]) {
                                            query, statistics, error in
                                            
                                            // ... process the results here
}

Objective-C

HKQuantityType *cumulativeActiveEnergyBurned =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
 
HKQuantityType *discreteHeartRate =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
 
// Cannot combine cumulative options with discrete options.
// However, you can combine a cumulative option and seperated by source
HKStatisticsQuery *cumulativeQuery =
[[HKStatisticsQuery alloc]
 initWithQuantityType:cumulativeActiveEnergyBurned
 quantitySamplePredicate:nil
 options:HKStatisticsOptionCumulativeSum | HKStatisticsOptionSeparateBySource
 completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
 
      // ... process the results here
 }];
 
// You can also combine any number of discrete options
// and the seperated by source option.
HKStatisticsQuery *discreteQuery =
[[HKStatisticsQuery alloc]
 initWithQuantityType:discreteHeartRate
 quantitySamplePredicate:nil
 options:HKStatisticsOptionDiscreteAverage | HKStatisticsOptionDiscreteMin |
 HKStatisticsOptionDiscreteMax | HKStatisticsOptionSeparateBySource
 completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
 
     // ... process the results here
 }];

Topics

Constants

Deprecated Constants

Initializers

See Also

Statistics