addQuantitiesFromSamples

addQuantitiesFromSamples

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

没有源代码,因此我正在即时编写它。由于他们没有讨论,因此我很难解决一种方法。

幸运的是,对于所有为锻炼项目添加样本量的锻炼类型,它都是相同的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视频的其他人所缺少的方法:



资料来源:Calories and Distance data from query

07-24 09:16