【发布时间】:2018-06-24 22:12:52
【问题描述】:
我正在尝试读取 HKWorkout 的路线数据,但我无法读取,因为当我尝试实例化 HKWorkoutRouteQuery我检索到的样本。
根据Apple's documentation,我的代码如下所示:
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 []
}
这对我来说毫无意义,因为 Apple 自己的文档要求 HKWorkoutRouteQuery 使用示例进行实例化。
任何帮助将不胜感激。非常感谢。
【问题讨论】:
标签: ios swift ios11 healthkit xcode9.4