I am working on an exercise app for the watchos 2 and creating streaming queries to retrieve the data for Heartrate, Distance (HKQuantityTypeIdentifierDistanceWalkingRunning) and Calories (HKQuantityTypeIdentifierActiveEnergyBurned) during the workout session by using the following model:
private func createCaloriesBurnedStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {
guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned) else { return nil }
let caloriesQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -> Void in
guard let newAnchor = newAnchor else {return}
self.anchorCalories = newAnchor
self.updateCalories(sampleObjects)
}
caloriesQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -> Void in
self.anchorCalories = newAnchor!
self.updateCalories(samples)
}
return caloriesQuery
}
The Heartrate data is coming right, however both calories and distance data seems to be coming in a non cumulative form with a very inconsistant values. In the middle of the workout I was getting 300-400 calories and by the end of the workout I got 14-18 cals.
Is there anything I need to do differently here?
Thanks... Alex