I'm trying to read the route data of an HKWorkout, but I can't because I get an error in my editor saying 'Argument passed to call that takes no arguments' when I try to instantiate an HKWorkoutRouteQuery
with my retrieved sample.
As per Apple's documentation, my code looks like this:
func getRouteData() -> [(Double)] {
// Return early if not a distance workout
guard self.shouldShowDistance else {
return []
}
let store = HKHealthStore()
let runningObjectQuery = HKQuery.predicateForObjects(from: self.workout)
let routeQuery = HKAnchoredObjectQuery(type: HKSeriesType.workoutRoute(), predicate: runningObjectQuery, anchor: nil, limit: HKObjectQueryNoLimit) { (query, samples, deletedObjects, anchor, error) in
guard error == nil else {
// Handle any errors here.
fatalError("query failed")
}
guard samples != nil else {
fatalError("No samples")
}
guard samples!.count > 0 else { fatalError("No samples") }
guard let route = samples?.first as? HKWorkoutRoute else {
fatalError("No samples")
}
// Create the route query.
let query = HKWorkoutRouteQuery(route: route) { (query, locationsOrNill, done, errorOrNil) in
// This block may be called multiple times.
if let error = errorOrNil {
// Handle any errors here.
return
}
guard let locations = locationsOrNil else {
fatalError("*** Invalid State: This can only fail if there was an error. ***")
}
// Do something with this batch of location data.
if done {
// The query returned all the location data associated with the route.
// Do something with the complete data set.
}
// You can stop the query by calling:
// store.stop(query)
}
store.execute(query)
}
store.execute(routeQuery)
return []
}
This makes no sense to me because Apple's own docs require HKWorkoutRouteQuery to be instantiated with a sample.
Any help would be much appreciated. Many thanks.
Apple got back to me through Bug Reporter. The issue is that I neglected to import CoreLocation. You can fix this error by adding the following line to the top of your file.
import CoreLocation
As an aside, I think this is a great example of the importance of writing descriptive errors. If the error had mentioned something about the resulting completion requiring CoreLocation
, I would have been able to solve this on my own.