Class

HKStatistics

Statistics objects represent the result of calculating the minimum, maximum, average, or sum over a set of samples in the HealthKit store. HealthKit creates statistic objects using either a statistics query or a statistics collection query. For the statistics query, it performs the specified calculations over all the samples that match the query. For the statistics collection query, it partitions the matching samples into a set of time intervals and performs the calculations over each interval separately.

Overview

By default, these queries automatically merge the data from all of your data sources before performing the calculations. If you want to merge the data yourself, you can set the separateBySource option. You can then request the statistical data for each source separately.

When requesting data from a statistics object, your request must match the options you used when creating the query. For example, if you create a query using the discreteAverage option, you must access the results using the averageQuantity() method.

For more information on calculating statistical data, see HKStatisticsQuery Class Reference. To calculate the statistics over a series of time intervals, see the HKStatisticsCollectionQuery Class Reference.

Topics

Getting Property Data

var startDate: Date

The start of the time period included in these statistics.

var endDate: Date

The end of the time period included in these statistics.

var quantityType: HKQuantityType

The quantity type of the samples used to calculate these statistics.

var sources: [HKSource]?

An array containing all the sources contributing to these statistics.

Getting Statistics Data

func averageQuantity()

Returns the average value from all the samples that match the query.

func averageQuantity(for: HKSource)

Returns the average value from all the samples that match the query and that were created by the specified source.

func maximumQuantity()

Returns the maximum value from all the samples that match the query.

func maximumQuantity(for: HKSource)

Returns the maximum value from all the samples that match the query and that were created by the specified source.

func minimumQuantity()

Returns the minimum value from all the samples that match the query.

func minimumQuantity(for: HKSource)

Returns the minimum value from all the samples that match the query and that were created by the specified source.

func sumQuantity()

Returns the sum of all the samples that match the query.

func sumQuantity(for: HKSource)

Returns the sum of all the samples that match the query and that were created by the specified source.

Constants

struct HKStatisticsOptions

Options for specifying which statistics to calculate.

Relationships

Inherits From