HKWorkout totalEnergyBurned doesn't correlate with daily sum of ActiveEnergyBurned.
This is more of a data structure question than a programming syntax question. The data structure of the Health app is somewhat of a black box to me.
I want to query HKHealthStore and create both a daily summary of items including, ActiveEnergyBurned, and also a summary of workouts including totalEnergyBurned.
I have code (below) that successfully retrieves this information. However the number for the daily total is usually LESS than that day's workout! I am sure that my code is not the problem, because the exact same numbers show up in the Apple Health app.
For example: yesterday's workout: My app workout.totalEnergyBurned = 905 kcal sum of yesterday's ActiveEnergyBurned 655 kcal Health app shows the exact same numbers for both. If ActiveEnergyBurned, doesn't include workouts, what does it include? I didn't have another 655 of ActiveEnergyBurned. It doesn't seem possible to me that ActiveEnergyBurned wouldn't include workouts.
func getActiveCalories(startDate:NSDate, endDate:NSDate){
let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)
let hkUnit = HKUnit.kilocalorieUnit()
getSumStatsFor(sampleType, hkUnit: hkUnit, startDate: startDate, endDate: endDate) { (hdObject, result) -> Void in
hdObject.activeCalories = result
}
}
func getSumStatsFor(quantityType:HKQuantityType, hkUnit: HKUnit, startDate:NSDate, endDate:NSDate, completionHandler:(HealthData, Double)->Void){
self.healthManager.getTotalsForDataType(quantityType, startDate: startDate, endDate: endDate) { (statisticsColl, error) -> Void in
statisticsColl.enumerateStatisticsFromDate(startDate, toDate: endDate, withBlock: { (statistics, stop) -> Void in
if let quantity = statistics.sumQuantity() {
let amount = quantity.doubleValueForUnit(hkUnit)
let filteredHDArray = self.healthDataArray.filter({ return self.isSameDate($0.date, dateTwo: statistics.startDate)})
if let healthPoint = filteredHDArray.first{
completionHandler(healthPoint, amount)
}
}
})
}
}
func getTotalsForDataType(quantitiyType:HKQuantityType, startDate:NSDate, endDate:NSDate, completion:(HKStatisticsCollection!, NSError!) -> Void){
println("getTotalsForDataType start: \(startDate) end: \(endDate)")
let dayStart = NSCalendar.currentCalendar().startOfDayForDate(startDate)
let addDay = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: 1, toDate: endDate, options:nil)
let dayEnd = NSCalendar.currentCalendar().startOfDayForDate(addDay!) /
let interval = NSDateComponents()
interval.day = 1
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.None)
let newQuery = HKStatisticsCollectionQuery(quantityType: quantitiyType,
quantitySamplePredicate: predicate,
options: HKStatisticsOptions.CumulativeSum,
anchorDate: dayStart,
intervalComponents: interval)
newQuery.initialResultsHandler = {
query, results, error in
if error != nil {
println("*** initialResultsHandler error calculating the statistics: \(error.localizedDescription) ***")
completion(nil, error)
}else{
completion(results,error)
}
}
/
newQuery.statisticsUpdateHandler = {
query, statistics, results, error in
println("*** updateHandler fired")
if error != nil {
println("*** updateHandler error calculating the statistics: \(error.localizedDescription) ***")
completion(nil, error)
}else{
completion(results,error)
}
}
self.healthKitStore.executeQuery(newQuery)
}
}
func readWorkouts(completion: (([AnyObject]!, NSError!) ->Void)!){
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: nil, limit: 0, sortDescriptors: [sortDescriptor]) { (sampleQuery, results, error) -> Void in
if let queryError = error {
println( "There was an error while reading the samples: \(queryError.localizedDescription)")
}
completion(results,error)
}
healthKitStore.executeQuery(sampleQuery)
}