I can't get any calories/activeEnergyBurned to show up in my app, and don't know why?
WorkoutInterfaceController
:
private func totalCalories() -> Double {
return totalEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
}
private func setTotalCalories(calories: Double) {
totalEnergyBurned = HKQuantity(unit: HKUnit.kilocalorie(), doubleValue: calories)
}
func startQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier) {
let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let updateHandler: ((HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) = { query, samples, deletedObjects, queryAnchor, error in
self.process(samples: samples, quantityTypeIdentifier: quantityTypeIdentifier)
}
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!,
predicate: queryPredicate,
anchor: nil,
limit: HKObjectQueryNoLimit,
resultsHandler: updateHandler)
query.updateHandler = updateHandler
healthStore.execute(query)
activeDataQueries.append(query)
}
func process(samples: [HKSample]?, quantityTypeIdentifier: HKQuantityTypeIdentifier) {
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self, !strongSelf.isPaused else { return }
if let quantitySamples = samples as? [HKQuantitySample] {
for sample in quantitySamples {
if quantityTypeIdentifier == HKQuantityTypeIdentifier.activeEnergyBurned {
let newKCal = sample.quantity.doubleValue(for: HKUnit.kilocalorie())
strongSelf.setTotalCalories(calories: strongSelf.totalCalories() + newKCal)
print("NewKCal: \(newKCal)")
print("TotalCalories: \(strongSelf.totalCalories())")
}
}
strongSelf.updateLabels()
}
}
}
The log prints out '0' no matter how long I run the app for.
I've tested on the Simulator and on Device.
Per a question, here is the code for saving workout data:
private func saveWorkout() {
// Create and save a workout sample
let configuration = workoutSession!.workoutConfiguration
let isIndoor = (configuration.locationType == .indoor) as NSNumber
print("locationType: \(configuration)")
let workout = HKWorkout(activityType: configuration.activityType,
start: workoutStartDate!,
end: workoutEndDate!,
workoutEvents: workoutEvents,
totalEnergyBurned: totalEnergyBurned,
totalDistance: nil,
metadata: [HKMetadataKeyIndoorWorkout:isIndoor]);
healthStore.save(workout) { success, _ in
if success {
self.addSamples(toWorkout: workout)
}
}
// Pass the workout to Summary Interface Controller
WKInterfaceController.reloadRootControllers(withNames: ["SummaryInterfaceController"], contexts: [workout])
}
private func addSamples(toWorkout workout: HKWorkout) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurned,
start: workoutStartDate!,
end: workoutEndDate!)
// Add samples to workout
healthStore.add([totalEnergyBurnedSample], to: workout) { (success: Bool, error: Error?) in
if success {
// Samples have been added
print("Samples have been added")
}
}
}