A query to access the location data stored in a workout route.


A route sample can contain a considerable amount of data (for example, several thousand CLLocation objects for a marathon). To manage this data, the query asynchronously returns the locations in batches. The location objects should have an accuracy of 50 meters or less; however, your app may want to perform additional smoothing before using the data.

To run a route query:

  1. Create a query object, providing a block to receive the location data.

  2. Call the HealthKit store’s execute(_:) method to run the query.

  3. Receive the route data. Your block receives one or more batches of location data. When the block’s done parameter is true, you have received all the data.

  4. (Optional) Call the HealthKit store’s stop(_:) method to stop the query early.

// Create the route query
let query = HKWorkoutRouteQuery(route: myRoute) { (query, locationsOrNill, done, errorOrNil) in
    // This block may be called multiple times.
    if let error = errorOrNil {
        // Handle the error here...
    guard let locations = locationsOrNil else {
        fatalError("*** Invalid State: This can only fail if we have an error. ***")
    // Do something with the locations here...
    if done {
        // The query has returned all the location data...
    // You can stop the query by calling:
    // store.stop(query)


Creating Route Queries

init(route: HKWorkoutRoute, dataHandler: (HKWorkoutRouteQuery, [CLLocation]?, Bool, Error?) -> Void)

Creates and returns a new query to access the location data associated with a workout route.


Inherits From

Conforms To

See Also