我使用这个功能来获取与RunKeeper同步后从健康套件燃烧的卡路里
func getActiveEnergy(currentDate: Date ,completion: @escaping ((_ totalEnergy: Double) -> Void)) {
let calendar = Calendar.current
var totalBurnedEnergy = Double()
let startOfDay = Int((currentDate.timeIntervalSince1970/86400)+1)*86400
let startOfDayDate = Date(timeIntervalSince1970: Double(startOfDay))
// Get the start of the day
let newDate = calendar.startOfDay(for: startOfDayDate)
let startDate: Date = calendar.date(byAdding: Calendar.Component.day, value: -1, to: newDate)!
// Set the Predicates
let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: newDate as Date, options: .strictStartDate)
// Perform the Query
let energySampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)
let query = HKSampleQuery(sampleType: energySampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
(query, results, error) in
if results == nil {
print("There was an error running the query: \(error)")
}
DispatchQueue.main.async {
for activity in results as! [HKQuantitySample]
{
let calories = activity.quantity.doubleValue(for: HKUnit.kilocalorie())
totalBurnedEnergy = totalBurnedEnergy + calories
}
completion(totalBurnedEnergy)
}
})
self.healthStore.execute(query)
}
问题是,如果我使用RunKeeper在一夜之间收集数据,我无法每天都得到分离的值。 示例:
4月17:3.1 4月18:4.2
但在我的应用程序中,我只得到以下数据:Apr 17:7.3
4月17号健康应用:
有什么想法吗?
解决了以下链接的问题:https://developer.apple.com/reference/healthkit/hkstatisticscollectionquery