HealthKit -> 查询附有路线的锻炼
Posted
技术标签:
【中文标题】HealthKit -> 查询附有路线的锻炼【英文标题】:HealthKit -> Query for workouts that have a route attached 【发布时间】:2021-02-24 09:07:05 【问题描述】:我想在HealthKit
中查询所有附加了workout-route
的workouts
。有没有办法用谓词做到这一点?
我通过查询来获取所有锻炼,然后查询该锻炼的路线,然后另一个查询来获取路线数据,从而使其工作... 但这对我来说似乎效率很低?!?
【问题讨论】:
你在这方面有什么进展吗?我正在为同样的问题苦苦挣扎,但我只能从 HealthKit 查询中获得分段的位置结果。下面的答案没有帮助,因为它只是文档的重复。 【参考方案1】:查询锻炼后需要查询锻炼路线和锻炼地点:
-
请求许可:
查询锻炼:
获取路径示例对象:
let runningObjectQuery = HKQuery.predicateForObjects(from: myWorkout)
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("The initial query failed.")
// Process the initial route data here.
routeQuery.updateHandler = (query, samples, deleted, anchor, error) in
guard error == nil else
// Handle any errors here.
fatalError("The update failed.")
// Process updates or additions here.
store.execute(routeQuery)
-
访问路径样本的位置数据:
// Create the route query.
let query = HKWorkoutRouteQuery(route: myRoute) (query, locationsOrNil, 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)
更多信息:https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/reading_route_data
【讨论】:
这只是文档的重复,您能否提供有关如何填写路线查询代码以将位置数据添加到地图的任何见解?以上是关于HealthKit -> 查询附有路线的锻炼的主要内容,如果未能解决你的问题,请参考以下文章
应用程序在后台时如何查询 HealthKit (HKWorkout) 更新?