当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Retrieve endDate of HKSample with HKStatisticsQuery

问题描述:

What I need to do is retrieve the minimum, maximum and the average heartrate using Healthkit.

With a simple HKStatisticsQuery i can easily get this information:

let healthStore = HKHealthStore()

let heartRateUnit = HKUnit(fromString: "count/min")

let heartRateType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!

var heartRateQuery:HKSampleQuery?

var heartRateAVG = 0

var heartRateMIN = 0

var heartRateMAX = 0

var heartRateAVGTime = ""

var heartRateMINTime = ""

var heartRateMAXTime = ""

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

queryHeartRate { () -> () in

print("self.heartRateAVG")

print("self.heartRateMIN")

print("self.heartRateMAX")

}

}

func queryHeartRate(completion: () -> Void) {

let avgHR = HKStatisticsOptions.DiscreteAverage

let minHR = HKStatisticsOptions.DiscreteMin

let maxHR = HKStatisticsOptions.DiscreteMax

let date = NSDate()

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "HH:mm"

let startOfDay = NSCalendar.currentCalendar().startOfDayForDate(date)

let components = NSDateComponents()

components.hour = 23

components.minute = 59

components.second = 59

let endOfDay = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: startOfDay, options: NSCalendarOptions(rawValue: 0))

let predicate = HKQuery.predicateForSamplesWithStartDate(startOfDay, endDate: endOfDay, options: [])

let statisticsAvgQuery = HKStatisticsQuery(quantityType: heartRateType, quantitySamplePredicate: predicate, options: avgHR) {

[unowned self] (query, result, error) in

if let avgBeat = result?.averageQuantity() {

dispatch_async(dispatch_get_main_queue(), {

let avgHeartRate = avgBeat.doubleValueForUnit(self.heartRateUnit)

self.heartRateAVG = Int(avgHeartRate)

completion()

})

}

}

let statisticsMinQuery = HKStatisticsQuery(quantityType: heartRateType, quantitySamplePredicate: predicate, options: minHR) {

[unowned self] (query, result, error) in

if let minBeat = result?.minimumQuantity() {

dispatch_async(dispatch_get_main_queue(), {

let minHeartRate = minBeat.doubleValueForUnit(self.heartRateUnit)

self.heartRateMIN = Int(minHeartRate)

completion()

})

}

}

let statisticsMaxQuery = HKStatisticsQuery(quantityType: heartRateType, quantitySamplePredicate: predicate, options: maxHR) {

[unowned self] (query, result, error) in

if let maxBeat = result?.maximumQuantity() {

dispatch_async(dispatch_get_main_queue(), {

let maxHeartRate = maxBeat.doubleValueForUnit(self.heartRateUnit)

self.heartRateMAX = Int(maxHeartRate)

completion()

})

}

}

healthStore.executeQuery(statisticsAvgQuery)

healthStore.executeQuery(statisticsMinQuery)

healthStore.executeQuery(statisticsMaxQuery)

}

The problem is that I extremely need the timestamp (HKSample endDate) of when the minimum and maximum bpm was collected.

An HKStatisticsQuery is not able to give me this parameter because the endDate of HKStatistics tells "This property contains the end date for the statistics. If you calculated these statistics using a statistics query, this is the latest end date from all the samples that match the query."

I need to do an HKSampleQuery, store all the heartrate readings into an array, reorder the readings in crescent order and estrapolate the endDate from the first and the last item, or there is a more simple solution?

I'm not considering something important?

分享给朋友:
您可能感兴趣的文章:
随机阅读: