First I prepare a long-run query to get new workouts:
self.exportQuery = HKObserverQuery(sampleType: sampleType, predicate: nil, updateHandler: { query, completionHandler, error in
self.queryWorkoutsToExport() {
completionHandler()
}
})
self.healthStore.execute(self.exportQuery!)
I enable the HealthStore background delivery with:
self.healthStore.enableBackgroundDelivery(for: HKObjectType.workoutType(), frequency: .immediate, withCompletion: nil)
Now I go outside for an outdoor running workout (with GPS trace). One my workout finished, the update handler of HKObserverQuery is called and I do a query to access the new workout:
let anchoredQuery = HKAnchoredObjectQuery(type: HKObjectType.workoutType(), predicate: predicate, anchor: anchor, limit: HKObjectQueryNoLimit) { [unowned self] query, newSamples, deletedSamples, newAnchor, error in
THE PROBLEM is that the workout is correctly returned in newSamples BUT I don't have any GPS data. The resultHandler in empty.
let workoutRoutesQuery = HKSampleQuery(sampleType: HKSeriesType.workoutRoute(),
predicate: HKQuery.predicateForObjects(from: workout),
limit: HKObjectQueryNoLimit,
sortDescriptors: [sort],
resultsHandler: { (query, samples, error) in
Anything wrong with my process? May be .immediate is to immediate in enableBackgroundDelivery is a bit too fast and I should move to .hourly. Because when I run the same code a bit later, I have the route date.