I have a custom WorkoutSessionManager.swift
that does not subclass WKInterfaceController
. It has its own context and protocol. It contains the query and sample updates. All the heart rate, distance and energy data (HKUnits) are printing to the console. This simple block outputs the data to the console.
guard let sample = activeEnergyBurnedSamples.first else{return}
let value = sample.quantity.doubleValueForUnit(self.energyUnit)
print(value)
I have a separate Dashboard.swift
with my mi, Cal, bpm labels.
Since it is live data is it possible to query the HK data directly without passing this value
property?
If this isn't possible how should I accomplish sending the value
to my labels in the external class?
WorkoutSessionManager.swift
func createActiveEnergyStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {
guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned) else {return nil}
let activeEnergyQuery = 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.addActiveEnergySamples(samples)
}
activeEnergyQuery.updateHandler = {(query, samples, deletedObjects, newAnchor, error) -> Void in
self.anchor = newAnchor!
self.addActiveEnergySamples(samples)
}
return activeEnergyQuery
}
func addActiveEnergySamples(samples: [HKSample]?) {
print("updating calorie samples")
guard let activeEnergyBurnedSamples = samples as? [HKQuantitySample] else { return }
dispatch_async(dispatch_get_main_queue()) {
self.currentActiveEnergyQuantity = self.currentActiveEnergyQuantity.addQuantitiesFromSamples(activeEnergyBurnedSamples, unit: self.energyUnit)
self.activeEnergySamples += activeEnergyBurnedSamples
self.delegate?.workoutSessionManager(self, didUpdateActiveEnergyQuantity: self.currentActiveEnergyQuantity)
// Checks
guard let sample = activeEnergyBurnedSamples.first else{return}
let value = sample.quantity.doubleValueForUnit(self.energyUnit)
print(value)
}
}
DashboardController.swift
@IBOutlet weak var milesLabel: WKInterfaceLabel!
@IBOutlet weak var caloriesLabel: WKInterfaceLabel!
@IBOutlet weak var bmpLabel: WKInterfaceLabel!