I am creating an app which the person can add an occasion of temperature (Writing to HKHealthStore), and the place I wish to question utilizing HKStatisticsQuery, discovering the discrete max and min for the interval of time, nonetheless it isn’t printing something. I attempted HKStatisticsQuery for different issues like BMI, however nothing outputs on the simulator.
var healthStore: HKHealthStore?
init() {
if HKHealthStore.isHealthDataAvailable() {
healthStore = HKHealthStore()
}
}
func writeTemp(startDate: Date, tempToAdd: Double) {
let tempType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!
let endDate = startDate + 60 * 60 // add 1h to startDate
let stepsSample = HKQuantitySample(kind: tempType, amount: HKQuantity.init(unit: HKUnit.degreeFahrenheit(), doubleValue: tempToAdd), begin: startDate, finish: endDate)
// After creating the pattern, we name healthStore.save()
if let healthStore = healthStore {
healthStore.save(stepsSample, withCompletion: { (success, error) -> Void in
if error != nil {
// one thing occurred
print("error = (String(describing: error))")
return
}
if success {
print("Steps efficiently saved in HealthKit")
return
} else {
// one thing occurred once more
print("Unhandled case!")
}
})
}
}
func calculateTemp(completion: @escaping (HKStatisticsQuery?) -> Void){
let healthStore = HKHealthStore()
var question: HKStatisticsQuery?
var query2: HKStatisticsQuery?
let tempType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!
let startDate = Calendar.present.date(byAdding: .day, worth: -3, to: Date())
let predicate = HKQuery.predicateForSamples(withStart: startDate, finish: Date(), choices: .strictStartDate)
var minima: Double?
var maxima: Double?
question = HKStatisticsQuery(quantityType: tempType, quantitySamplePredicate: predicate, choices: .discreteMax){ (question, outcomes, error) in
// minima = outcomes?.minimumQuantity()?.doubleValue(for: HKUnit.degreeFahrenheit())
// print(minima!)
print(error!)
print("HI")
}
query2 = HKStatisticsQuery(quantityType: tempType, quantitySamplePredicate: predicate, choices: .discreteMin){(question, outcomes, error) in
// maxima = outcomes?.maximumQuantity()?.doubleValue(for: HKUnit.degreeFahrenheit())
// print(maxima!)
print(error!)
print("HI")
}
healthStore.execute(question!)
healthStore.execute(query2!)
}
func requestAuthorization(completion: @escaping (Bool) -> Void) {
let tempType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyTemperature)!
guard let healthStore = self.healthStore else { return completion(false) }
healthStore.requestAuthorization(toShare: [tempType], learn: [tempType]) { (success, error) in
completion(success)
}
}
func testStatisticsQueryDiscrete() {
guard let bodyMassType = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
fatalError("*** Unable to get the physique mass kind ***")
}
let question = HKStatisticsQuery.init(quantityType: bodyMassType,
quantitySamplePredicate: nil,
choices: [HKStatisticsOptions.discreteMax, HKStatisticsOptions.separateBySource]) { (question, outcomes, error) in
print("Complete: (String(describing: outcomes?.maximumQuantity()?.doubleValue(for: HKUnit.pound())))")
for supply in (outcomes?.sources)! {
print("Seperate Supply: (outcomes?.maximumQuantity(for: supply)?.doubleValue(for: HKUnit.pound()))")
}
}
healthStore?.execute(question)
}
func check(){
guard let bodyMassType = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
fatalError("*** Unable to get the physique mass kind ***")
}
print(bodyMassType)
let question = HKStatisticsQuery.init(quantityType: bodyMassType,
quantitySamplePredicate: nil,
choices: [HKStatisticsOptions.discreteMax, HKStatisticsOptions.separateBySource]) { (question, outcomes, error) in
print("Complete: (String(describing: outcomes?.maximumQuantity()?.doubleValue(for: HKUnit.pound())))")
self.healthStore?.execute(question)
}
}
}
May somebody please give any doable nudges on what is going on on?