Post not yet marked as solved
Post marked as unsolved with 0 replies, 463 views
Hi,
I am writing workout app for WatchOS and want to add activeEnergyBurned sample whenever user has finished exercise in workout.
But whenever I add sample to builder it returns that it was successfully added but when checking added samples in Health app, it shows that it is always 0 and also sum of samples is also always 0.
Does activeEnergyBurned has some additional requirements because when tried to write basalEnergyBurned it successfully wrote given calories.
Writing sample here:
let energyBurnedType = HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)
if energyBurnedType != nil {
let energyBurned = HKQuantity(unit: HKUnit.largeCalorie(), doubleValue: Double(totalCalories))
let energyBurnedSample = HKQuantitySample(type: energyBurnedType!, quantity: energyBurned, start: self.exerciseStart, end: Date())
builder.add([energyBurnedSample]) { (success, error) in
print(success)
print(error)
}
}
And reading data that always returns 0:
func updateForStatistics(_ statistics: HKStatistics?) {
guard let statistics = statistics else { return }
DispatchQueue.main.async {
switch statistics.quantityType {
case HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned):
let energyUnit = HKUnit.largeCalorie()
let value = statistics.sumQuantity()?.doubleValue(for: energyUnit)
self.activeCalories = Double( round( 1 * value! ) / 1 )
print(value)
return
default:
return
}
}
}
Any ideas on what am I missing?
I checked my workout recordings and I see that samples get added but doesn't affect total active energy shown in Health app:
And here you can see that sample is added: