Can't get Min/Max/Avg Heartbeat HKUnit.count

So, I have three statistics queries running to get the min, max and average heartbeat per minute over the past two days of samples.


The query "sort of" runs fine in terms of it returns a results object with a value (Value looks wrong, but not the point at this point)


But when I call resultQuantity.doubleValueForUnit(HKUnit.countUnit()) if throws a NSInvalidArgumentException


Extension[25004:6643049] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to convert incompatible units: count/s, count'


I don't understand what is wrong here. Here is my code bolding the line that throws the exception.


func getMaximumHeartBeatData() {

let predicate = getHeartBeatPastTwoDaysPredicate()

createAndExecuteQueries(predicate, option: HKStatisticsOptions.DiscreteMax) { (query,

results, error) -> Void in

if let avgHeart = results?.maximumQuantity() {

self.maximumHeartBeat = Int(avgHeart.doubleValueForUnit(HKUnit.countUnit()))

} else {

self.maximumHeartBeat = 0

}

}

}

func createAndExecuteQueries(predicate: NSPredicate, option: HKStatisticsOptions, handler: (HKStatisticsQuery, HKStatistics?, NSError?) -> Void) {

let query = HKStatisticsQuery(quantityType: heartRateType, quantitySamplePredicate: predicate, options:[option], completionHandler: handler)

healthStore.executeQuery(query)

}


func getHeartBeatPastTwoDaysPredicate() -> NSPredicate! {

let endDate = NSDate()

let startDate = getDateTwoDaysBefore(endDate)

return HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: [])

}


func getDateTwoDaysBefore(today: NSDate) -> NSDate {

let calendar = NSCalendar.currentCalendar()

let endDate = calendar.dateByAddingUnit(NSCalendarUnit.Day, value: -2, toDate: today, options: [])

return endDate!

}

Answered by Bytor99999 in 54478022

Resolved.


Int(avgHeart.doubleValueForUnit(HKUnit.init(fromString: "count/s"))*60)

Accepted Answer

Resolved.


Int(avgHeart.doubleValueForUnit(HKUnit.init(fromString: "count/s"))*60)

Can't get Min/Max/Avg Heartbeat HKUnit.count
 
 
Q