Contents

predicateForWorkoutActivities(operatorType:quantityType:sumQuantity:)

Returns a predicate for matching workout activities based the sum of an associated quantity type.

Declaration

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

Parameters

  • operatorType:

    The operator type to use when comparing the sum.

  • quantityType:

    The type of Hkquantitysample objects used to calculate the sum.

  • sumQuantity:

    The target value for the sum.

Discussion

Use this convenience method to create a predicate that matches workout activities with the specified total value. To use this predicate, call predicateForWorkouts(activityPredicate:) to wrap this predicate inside a workout predicate. You can then use the workout predicate in your query.

The following sample creates a predicate for workout activities with a total step count of 1000 or higher.

let quantityType = HKQuantityType(.stepCount)

let expectedQuantity = HKQuantity(unit: .count(),
                                  doubleValue: 1000.0)

let stepCountPredicate =
HKQuery.predicateForWorkoutActivities(
    operatorType: .greaterThanOrEqualTo,
    quantityType: quantityType,
    sumQuantity:
        expectedQuantity
)

// Wrap the activity predicate inside a workout predicate.
let workoutPredicate = HKQuery.predicateForWorkouts(activityPredicate: stepCountPredicate)

For more information on how HealthKit calculates statistics for HKWorkoutActivity objects, see statistics(for:).

See Also

Creating workout activity predicates