HomeiOS Developmentios - Can somebody assist me with my updateHandler for lengthy operating...

ios – Can somebody assist me with my updateHandler for lengthy operating queries?


I can not seem to wrap my head round with lengthy operating queries in HealthKit when it comes coping with the replace handlers. I ask as a result of from my finish once I add my code to the bodily system, my knowledge is reloaded and appending to my array which is including further knowledge to the display screen. From my understanding the updateHandlers screens for brand spanking new knowledge/values within the background to replace the worth in a brand new view. I am making an attempt to imitate the Apple Health/Well being app the place the worth updates and shows it to the person with out having to shut the app or swap views. I attempted to be concise with my code primarily to get an thought of the updateHandler. I attempted stopping the question and .onDisappear modifier however the .onDisappear would not replace the values except I power closed the app and opened it once more.

//Properties inside my view mannequin for resting HR.
var restingHRquery: HKStatisticsCollectionQuery?
@Revealed var restingHR: [RestingHeartRate] = [RestingHeartRate]()


    func calculateRestingHRData() {
        
        let restingHRpredicate = HKQuery.predicateForSamples(withStart: oneWeekAgo, finish: nil, choices: .strictStartDate)
        
        
        restingHRquery =   HKStatisticsCollectionQuery(quantityType: restingHeartRateType,
                                                       quantitySamplePredicate: restingHRpredicate,
                                                       choices: .discreteAverage,
                                                       anchorDate: anchorDate,
                                                       intervalComponents: day by day)
        
        
        restingHRquery!.initialResultsHandler = {
            restingQuery, statisticsCollection, error in
            
            //Deal with errors right here
            if let error = error as? HKError {
                swap (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, cease in
                if let restHRquantity = statistics.averageQuantity() {
                    let hrdate = statistics.startDate
                    
                    //HR Items
                    let hrUnit = HKUnit(from: "depend/min")
                    let restHRvalue = restHRquantity.doubleValue(for: hrUnit)
                    let restHR = RestingHeartRate(restingValue: Int(restHRvalue), date: hrdate)
                    
                    DispatchQueue.major.async {
                        self.restingHR.append(restHR)
                    }
                }
            }
        }
        
        restingHRquery!.statisticsUpdateHandler = {
            restingQuery, statistics, statisticsCollection, error in
            
            //Deal with errors right here
            if let error = error as? HKError {
                swap (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, cease in
                if let restHRquantity = statistics.averageQuantity() {
                    let hrdate = statistics.startDate
                    
                    //HR Items
                    let hrUnit = HKUnit(from: "depend/min")
                    let restHRvalue = restHRquantity.doubleValue(for: hrUnit)
                    let restHR = RestingHeartRate(restingValue: Int(restHRvalue), date: hrdate)
                    
                    DispatchQueue.major.async {
                        self.restingHR.append(restHR)
                    }
                }
            }
        }
        
        
        guard let restingHRquery = self.restingHRquery else { return }
      
        self.healthStore?.execute(restingHRquery)
    }

My chart at all times being up to date which causes it to reload my checklist and draw new knowledge on the charts which makes the road go loopy. I am utilizing the EnvironmentObject as a result of it is a subview. I am simply getting straight to the chart view

struct OneWeekRestHRChartView: View {
    @EnvironmentObject var healthStoreVM: HealthStoreViewModel
    
    var physique: some View {
        
        VStack(alignment: .main, spacing: 10) {
      
            Textual content("Common: (healthStoreVM.averageRestHR) bpm")
                .font(.headline)
            
            Chart {
                ForEach(healthStoreVM.restingHR.reversed(), id: .date) {
                    restHrData in
                    
                    LineMark(x: .worth("day", restHrData.date, unit: .day),
                             y: .worth("RHR", restHrData.restingValue)
                    )
                    .interpolationMethod(.catmullRom)
                    .foregroundStyle(.crimson)
                    .image() {
                        Circle()
                            .fill(.crimson)
                            .body(width: 15)
                    }
                }
            }
            .body(peak: 200)
            .chartYScale(area: 30...80)
            .chartXAxis {
                AxisMarks(values: .stride(by: .day)) {
                    AxisGridLine()
                    AxisValueLabel(format: .dateTime.day().month(), centered: true)
                    
                }
            }
        }
        .padding(.horizontal)
        .navigationTitle("Resting Coronary heart Price")

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

Might this simply be a SwiftUI chart challenge?

enter image description here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments