如何在 HealthKit 中“addQuantitiesFromSamples”?

How to `addQuantitiesFromSamples` in HealthKit?

基于 WWDC 2015 - Session 203 构建一个 HealthKit/WatchKit 应用程序。

没有源代码,所以我是即时编写的。有一种方法我很难用,因为他们不讨论它。

幸运的是,所有的锻炼类型都使用相同的 addQuantitiesFromSamples 方法将样本量添加到锻炼课程中。

我当然有这个错误,因为我的代码中不存在该方法。

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

我不确定如何编写添加样本数量的方法。该方法必须是相对基础的,因为它被用于项目中的所有三个示例查询。

sumDistanceSamples函数就是调用神秘addQuantitiesFromSamples方法的地方。

这是包含相同错误的三个块之一,所以我只需要为其中一个找到解决方案。

WorkoutSessionManager.swift

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
        self.sumDistanceSamples(samples)
    }

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

    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)

    }
}

// MARK: HEART RATE STREAMING
func createHearRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {

    // alternative method to creating a match samples predicate

    // Append the new quantities with the current heart rate quantity.
    guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else {return nil}

    // Instantiate a HKAnchoredObjectQuery object with a results handler that calls our sumHeartRateSamples function
    let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, samples, deletedObjectts, newAnchor, error) -> Void in
        guard let newAnchor = newAnchor else {return}
        self.anchor = newAnchor
        self.updateHeartRateSamples(samples)
    }

    // Results handler that calls our addActiveEnergySamples function
    heartRateQuery.updateHandler = {(query, samples, deletedObjects, newAnchor, error) -> Void in
        self.anchor = newAnchor!
        self.updateHeartRateSamples(samples)
    }

    return heartRateQuery
}

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

    // updateHeartRateSamples method dispatches back to the main queue.
    dispatch_async(dispatch_get_main_queue()) { 

       // Error: Value of type 'HKQuantitySample?' has no member 'addQuantitiesFromSamples
       self.currentHeartRateSample = self.currentHeartRateSample.addQuantitiesFromSamples(heartRateCountSamples, unit: self.countPerMinuteUnit)

        // appends/updates that sample to an array of samples accumulated over the workout.
        self.heartRateSamples += heartRateCountSamples

        self.delegate?.workoutSessionManager(self, didUpdateHeartRateSample: self.currentHeartRateSample!)
    }

}

我不确定这是否是您要查找的内容,但这似乎是观看同一 WWDC 视频的其他人所缺少的方法:

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
    }
}

来源:Calories and Distance data from query