@incmiko’s answer pointed me in the right direction by looking at HKSampleQuery
. However his/her code did not help me. So here is what I did to get the stroke style per lap.
private func querySwimStrokes(for sample: HKSample) {
let sampleType = HKSampleType.quantityType(forIdentifier: .swimmingStrokeCount)!
let sampleDate = HKQuery.predicateForSamples(withStart: sample.startDate, end: sample.endDate, options: [])
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let query = HKSampleQuery(sampleType: sampleType,
predicate: sampleDate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [sortDescriptor]) { query, samplesOrNil, errorOrNil in
samplesOrNil?.forEach({ sample in
if let quantitySample = sample as? HKQuantitySample {
let strokes = quantitySample.quantity.doubleValue(for: HKUnit.count())
print(strokes)
}
if let strokeStyleInt = sample.metadata?["HKSwimmingStrokeStyle"] as? Int,
let strokeStyle = HKSwimmingStrokeStyle(rawValue: strokeStyleInt){
print(strokeStyle)
}
})
}
HKHealthStore().execute(query)
}
CLICK HERE to find out more related problems solutions.