我正在创建一个 iWatch 应用程序,它读取心率数据,然后我用它来绘制一些图表。应用程序中有 2 个按钮,一个用于暂停锻炼,另一个用于恢复锻炼。单击暂停按钮时,我正在调用pauseWorkoutSession
healthStore 的方法:
healthStore.pauseWorkoutSession(workoutSession!)
OnClick 的 Resume Button,我正在调用resumeWorkoutSession
healthStore 的方法:
healthStore.resumeWorkoutSession(workoutSession!)
要获取心率数据,我正在运行以下代码:
let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
let datePredicate = HKQuery.predicateForSamplesWithStartDate(instanceDel.workoutSessionStartDate, endDate: nil, options: .StrictStartDate)
let updateHandler: (HKAnchoredObjectQuery,[HKSample]?,[HKDeletedObject]?,HKQueryAnchor?,
NSError?)-> Void = {query, samples, deleteObjects, newAnchor, error in
if !self.instanceDel.workoutPaused {
//do something
}
}
heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: datePredicate, anchor: nil, limit: Int(HKObjectQueryNoLimit) , resultsHandler: updateHandler)
heartRateQuery!.updateHandler = updateHandler
instanceDel.healthStore.executeQuery(heartRateQuery!)
我面临的问题是更新处理程序不断被调用,即使锻炼暂停,新的心率值也会不断添加到样本数组中。究竟是pauseWorkoutSession
做什么的?是不是应该暂停锻炼以获取新的心率读数?