Return from query in swift

Viewed 21

So I'm pretty new to Swift and I'm trying to understand how to return from the follow execute(query) and update the result in ContentView.

func latestHarteRate() {

    guard let sampleType = HKObjectType.quantityType(forIdentifier: .heartRate) else {
        return
    }
    let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())

    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictEndDate)

    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)

    let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]){(sample,result,error) in guard error == nil else{
            return
        }


    let data = result![0] as! HKQuantitySample
    let unit = HKUnit(from: "count/min")
    let latestHr = data.quantity.doubleValue(for: unit)
    

    print(latestHr)
    }
    
    healthStore?.execute(query)
    
}

}

0 Answers
Related