swift 有人可以帮助我处理长时间运行的查询的updateHandler吗?

wbrvyc0a  于 2023-04-19  发布在  Swift
关注(0)|答案(1)|浏览(91)

当涉及到更新处理程序时,我似乎无法在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图表问题吗?

epggiuax

epggiuax1#

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)
                    }
                }
            }
        }

相关问题