Contents

predicateForWorkouts(operatorType:quantityType:averageQuantity:)

Returns a predicate for matching workouts based the average value of an associated quantity type.

Declaration

class func predicateForWorkouts(operatorType: NSComparisonPredicate.Operator, quantityType: HKQuantityType, averageQuantity: HKQuantity) -> NSPredicate

Parameters

  • operatorType:

    The operator type to use when comparing the quantity.

  • quantityType:

    The type of Hkquantitysample objects used to calculate the average quantity.

  • averageQuantity:

    The target value for the average quantity.

Discussion

Use this convenience method to create a predicate that matches workouts with the specified average quantity. For more information on how HealthKit calculates statistics for HKWorkout objects, see statistics(for:) .

The following sample creates a predicate for workouts with an average heart rate of 150 bmp or higher.

let quantityType = HKQuantityType(.heartRate)

let expectedQuantity =
HKQuantity(unit: .count().unitDivided(by: .minute()),
           doubleValue: 150.0)

let heartRatePredicate = HKQuery.predicateForWorkouts(
    operatorType: .greaterThanOrEqualTo,
    quantityType: quantityType,
    averageQuantity: expectedQuantity)

See Also

Creating workout predicates