tymac tymac - 2 months ago 15x
Swift Question

How to `addQuantitiesFromSamples` in HealthKit?

Building a HealthKit/WatchKit app based off WWDC 2015 - Session 203.

There is no source code so I am writing it on the fly. There is a method I am having difficulty with since they don't discuss it.

Luckily it's the same

method for all the workout types that add sample quantities to the workout session.

Of course I have this error because that method does not exist in my code.

Value of type 'HKQuantity' has no member 'addQuantitiesFromSamples'

I am not sure how to write a method that adds sample quantities. The method must be relatively basic because it's being used on all three of the sample queries in the project.

function is where the mystery
method is called.

This is one of the three blocks containing the same error so I just need to find a solution for one of them.


class WorkoutSessionManager: NSObject, HKWorkoutSessionDelegate {

var activeEnergySamples: [HKQuantitySample] = []
var distanceSamples: [HKQuantitySample] = []
var heartRateSamples: [HKQuantitySample] = []

// ... code

var distanceType: HKQuantityType {
if self.workoutSession.activityType == .Cycling {
return HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling)!
} else {
return HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!

var currentActiveEnergyQuantity: HKQuantity
var currentDistanceQuantity: HKQuantity
var currentHeartRateSample: HKQuantitySample?

// ... code

// MARK: Data queries

// Create streaming query helper method.
func createStreamingDistanceQuery(workoutStartDate: NSDate) -> HKQuery? {
guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning) else {return nil}

// Instantiate a HKAnchoredObjectQuery object with a results handler.
let distanceQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, samples, deletedObjects, newAnchor, error) -> Void in
guard let newAnchor = newAnchor else {return}
self.anchor = newAnchor

// Results handler that calls sumDistanceSamples function.
distanceQuery.updateHandler = {(query, samples, deletedObjects, newAnchor, error) -> Void in
self.anchor = newAnchor!

return distanceQuery

func sumDistanceSamples(samples: [HKSample]?) {
guard let currentDistanceSamples = samples as? [HKQuantitySample] else { return }

dispatch_async(dispatch_get_main_queue()) {

// Error point - "no member 'addQuantitiesFromSamples'"
self.currentDistanceQuantity = self.currentDistanceQuantity.addQuantitiesFromSamples(currentDistanceSamples, unit: self.distanceUnit)

// Add sample to array of samples accumulated over the workout.
self.distanceSamples += currentDistanceSamples

self.delegate?.workoutSessionManager(self, didUpdateDistanceQuantity: self.currentDistanceQuantity)



I'm not sure if this is what you're looking for, but this appears to be your missing method from someone else who watched the same WWDC video:

extension HKQuantity {
    func addQuantitiesFromSamples(samples : [HKQuantitySample], unit: HKUnit) -> HKQuantity  {

        var currentValue = doubleValueForUnit(unit)

        for sample in samples {
            let value = sample.quantity.doubleValueForUnit(unit)
            currentValue += value
        let result = HKQuantity(unit: unit, doubleValue: currentValue)
        return result

Source: Calories and Distance data from query