ios 使用HealthKit updateHandler时,如何修复添加到现有数据中的数据?

ercv8c1e  于 2023-05-30  发布在  iOS
关注(0)|答案(1)|浏览(218)

在处理更新处理程序时,我似乎无法在HealthKit中处理长时间运行的查询。我问,因为从我的结束时,我上传我的代码到物理设备,我的数据被重新加载和追加到我的数组,这是添加额外的数据到屏幕上。根据我的理解,updateHandlers在后台监视新数据/值,以更新新视图中的值。我试图模仿苹果健身/健康应用程序,其中值更新并显示给用户,而不必关闭应用程序或切换视图。我尽量使代码简洁,主要是为了了解updateHandler。我尝试停止查询和.onDisappear修饰符,但.onDisappear不会更新值,除非我强制关闭应用程序并再次打开它。

//Properties within my view model for resting HR.
var restingHRquery: HKStatisticsCollectionQuery?
@Published var restingHR: [RestingHeartRate] = [RestingHeartRate]()

    func calculateRestingHRData() {
        
        let restingHRpredicate = HKQuery.predicateForSamples(withStart: oneWeekAgo, end: nil, options: .strictStartDate)
        
        
        restingHRquery =   HKStatisticsCollectionQuery(quantityType: restingHeartRateType,
                                                       quantitySamplePredicate: restingHRpredicate,
                                                       options: .discreteAverage,
                                                       anchorDate: anchorDate,
                                                       intervalComponents: daily)
        
        
        restingHRquery!.initialResultsHandler = {
            restingQuery, 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 resting HR
            statisticsCollection.enumerateStatistics(from: self.startDate, to: self.date) { statistics, stop in
                if let restHRquantity = statistics.averageQuantity() {
                    let hrdate = statistics.startDate
                    
                    //HR Units
                    let hrUnit = HKUnit(from: "count/min")
                    let restHRvalue = restHRquantity.doubleValue(for: hrUnit)
                    let restHR = RestingHeartRate(restingValue: Int(restHRvalue), date: hrdate)
                    
                    DispatchQueue.main.async {
                        self.restingHR.append(restHR)
                    }
                }
            }
        }
        
        restingHRquery!.statisticsUpdateHandler = {
            restingQuery, 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}
            
            //Calculating resting HR
            statisticsCollection.enumerateStatistics(from: self.startDate, to: self.date) { statistics, stop in
                if let restHRquantity = statistics.averageQuantity() {
                    let hrdate = statistics.startDate
                    
                    //HR Units
                    let hrUnit = HKUnit(from: "count/min")
                    let restHRvalue = restHRquantity.doubleValue(for: hrUnit)
                    let restHR = RestingHeartRate(restingValue: Int(restHRvalue), date: hrdate)
                    
                    DispatchQueue.main.async {
                        self.restingHR.append(restHR)
                    }
                }
            }
        }
        
        
        guard let restingHRquery = self.restingHRquery else { return }
      
        self.healthStore?.execute(restingHRquery)
    }

我的图表总是被更新,这导致它重新加载我的列表,并在图表上绘制新的数据,这使得线变得疯狂。我使用EnvironmentObject是因为它是一个子视图。我只是直接进入图表视图

struct OneWeekRestHRChartView: View {
    @EnvironmentObject var healthStoreVM: HealthStoreViewModel
    
    var body: some View {
        
        VStack(alignment: .leading, spacing: 10) {
      
            Text("Average: \(healthStoreVM.averageRestHR) bpm")
                .font(.headline)
            
            Chart {
                ForEach(healthStoreVM.restingHR.reversed(), id: \.date) {
                    restHrData in
                    
                    LineMark(x: .value("day", restHrData.date, unit: .day),
                             y: .value("RHR", restHrData.restingValue)
                    )
                    .interpolationMethod(.catmullRom)
                    .foregroundStyle(.red)
                    .symbol() {
                        Circle()
                            .fill(.red)
                            .frame(width: 15)
                    }
                }
            }
            .frame(height: 200)
            .chartYScale(domain: 30...80)
            .chartXAxis {
                AxisMarks(values: .stride(by: .day)) {
                    AxisGridLine()
                    AxisValueLabel(format: .dateTime.day().month(), centered: true)
                    
                }
            }
        }
        .padding(.horizontal)
        .navigationTitle("Resting Heart Rate")

        
        List{
            ForEach(healthStoreVM.restingHR.reversed(), id: \.date) {
                restHR in
                
                DataListView(imageText: "heart.fill",
                             imageColor: .red,
                             valueText: "\(restHR.restingValue) bpm",
                             date: restHR.date)
            }
        }
        .listStyle(.inset)
    }
}

这可能只是SwiftUI图表问题吗?

mu0hgdu0

mu0hgdu01#

statisticsUpdateHandler不会仅使用新结果调用。调用它时会显示更新后的完整结果集。
在再次添加结果之前,需要从restingHR中清除现有结果。

restingHRquery!.statisticsUpdateHandler = {
            restingQuery, 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}

            DispatchQueue.main.async {

                self.restingHR.removeAll()
            
            //Calculating resting HR
                statisticsCollection.enumerateStatistics(from: self.startDate, to: self.date) { statistics, stop in
                    if let restHRquantity = statistics.averageQuantity() {
                        let hrdate = statistics.startDate
                    
                        //HR Units
                        let hrUnit = HKUnit(from: "count/min")
                        let restHRvalue = restHRquantity.doubleValue(for: hrUnit)
                        let restHR = RestingHeartRate(restingValue: Int(restHRvalue), date: hrdate)
                    
                        self.restingHR.append(restHR)
                    }
                }
            }
        }

相关问题