检索锻炼时间时,我的 updateHandler 出现问题。事先,我的锻炼时间会更新,在新的一天开始时会重置为零。现在,我的锻炼时间仍然显示前一天的值,并且不会在新的一天重置为零。看来我必须实际获得一些锻炼时间才能实际更新。直到最近我才遇到任何问题,而且从一开始我就没有更改过我的代码。在查看刚刚检索过去 7 天数据的图表时,我注意到了这个问题。步数会更新到新的一天,但不会更新我的运动分钟图。我试过设置断点并查看日期是否有问题,但在检索锻炼时间时没有问题。我还将它与步数数据的逻辑进行了比较,两者都很相似。我有我的 ViewModel 和用于显示锻炼分钟数的视图。我还会附上我的图表图像给你看。
class HealthStoreViewModel: ObservableObject {
var healthStore: HKHealthStore?
let exerciseTimeType = HKQuantityType.quantityType(forIdentifier: .appleExerciseTime)!
var exerciseTimeQuery: HKStatisticsCollectionQuery?
@Published var exerciseTime: [ExerciseTime] = [ExerciseTime]()
//Computed properties
var currentExTime: Int {
exerciseTime.last?.exerValue ?? 0
}
//This is for the week starting Sunday - Saturday
var weeklyExTime: Int {
exerciseTime.reduce(0) { $0 + $1.exerValue }
}
let anchorDate = Date.sundayAt12AM()
let daily = DateComponents(day: 1)
init(){
if HKHealthStore.isHealthDataAvailable(){
healthStore = HKHealthStore()
showAllExerciseData()
} else {
print("HealthKit is unavailable on this platform")
}
}
func requestUserAuthorization() {
let healthTypes = Set([stepType, restingHeartRateType, hrvType, exerciseTimeType, caloriesBurnedType, workoutType])
guard let healthStore = self.healthStore else {
return
}
healthStore.requestAuthorization(toShare: [], read: healthTypes) { success, error in
if success {
self.showAllExerciseData()
}
}
}
func showAllExerciseData() {
self.calculateSevenDaysExerciseTime()
//There's more methods to retrieve other health stats, but only showing exercise time here.
}
func calculateSevenDaysExerciseTime() {
let startDate = Calendar.current.dateInterval(of: .weekOfYear, for: Date())?.start ?? Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
exerciseTimeQuery = HKStatisticsCollectionQuery(quantityType: exerciseTimeType,
quantitySamplePredicate: predicate,
options: .cumulativeSum,
anchorDate: anchorDate,
intervalComponents: daily)
exerciseTimeQuery!.initialResultsHandler = {
exerciseTimeQuery, statisticsCollection, error in
//Handle errors here
if let error = error as? HKError {
switch (error.code) {
case .errorHealthDataUnavailable:
return
case .errorNoData:
return
default:
return
}
}
guard let statisticsCollection = statisticsCollection else { return}
//Calculating exercise time
statisticsCollection.enumerateStatistics(from: startDate, to: self.date) { statistics, stop in
if let exerciseTimequantity = statistics.sumQuantity() {
let exerciseTimedate = statistics.startDate
//Exercise Time
let exerciseTimevalue = exerciseTimequantity.doubleValue(for: .minute())
let exTime = ExerciseTime(exerValue: Int(exerciseTimevalue), date: exerciseTimedate)
DispatchQueue.main.async {
self.exerciseTime.append(exTime)
}
}
}
}
exerciseTimeQuery!.statisticsUpdateHandler = {
exerciseTimeQuery, statistics, statisticsCollection, error in
//Handle errors here
if let error = error as? HKError {
switch (error.code) {
case .errorHealthDataUnavailable:
return
case .errorNoData:
return
default:
return
}
}
guard let statisticsCollection = statisticsCollection else { return }
statisticsCollection.enumerateStatistics(from: startDate, to: self.date) { statistics, stop in
if let exerciseTimequantity = statistics.sumQuantity() {
let exerciseTimedate = statistics.startDate
//Exercise Time
let exerciseTimevalue = exerciseTimequantity.doubleValue(for: .minute())
let exTime = ExerciseTime(exerValue: Int(exerciseTimevalue), date: exerciseTimedate)
DispatchQueue.main.async {
self.exerciseTime.append(exTime)
}
}
}
}
guard let exerciseTimeQuery = self.exerciseTimeQuery else { return }
self.healthStore?.execute(exerciseTimeQuery)
}
}
我的视图显示锻炼记录
HStack(spacing: 50) {
ExerciseGaugeView(progress: Double(healthStoreVM.currentExTime), minValue: 0.0, maxValue: Double(healthStoreVM.exerciseDayGoal), title: "Today", dateText: Constants.todayDateString)
ExerciseGaugeView(progress: Double(healthStoreVM.weeklyExTime),
minValue: 0.0,
maxValue: Double(healthStoreVM.exerciseWeeklyGoal),
title: "Weekly Total", dateText: Constants.currentWeekDatesString)
}
看看4月3日的步数是怎么显示的,锻炼时间还没更新? 73运动分钟应该归零了