问题描述
Apple health应用程序按日期明智地提供数据,如下图所示.
通过使用HealthKit
,我正在从Apple Health中获取步骤数据
By using HealthKit
i am fetching the steps data from apple healthas
let p1 = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
let p2 = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: true)
let timeSortDesriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)//as in health kit entry
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let predicate = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
let sourceQuery = HKSourceQuery(sampleType: quantityType, samplePredicate: predicate, completionHandler: { query,sources,error in
if sources?.count != 0 && sources != nil {
let lastIndex = sources!.count - 1
var sourcesArray = Array(sources!)
for i in 0..<sourcesArray.count {
let sourcePredicate = HKQuery.predicateForObjects(from: sourcesArray[i])
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p1, p2,sourcePredicate])
let query = HKSampleQuery(sampleType: quantityType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [timeSortDesriptor], resultsHandler: { query, results, error in
guard let samples = results as? [HKQuantitySample] else {
return
}......
sourceQuery提供了多个对象,例如Apple Watch,My iPhone.此外,我正在使用HKSampleQuery
的for循环,该循环给出了HKQuantitySample
对象.问题是[HKQuantitySample]
给出未按日期排序的步骤数据数组.我正在寻找像健康"应用程序中的苹果健康显示日期那样的日期数据.
sourceQuery gives multiple object like Apple watch, My iPhone.further i am using for loop with HKSampleQuery
which gives HKQuantitySample
object.Problem is [HKQuantitySample]
gives Array of step data which is not sorted datewise.I am looking for data which are club for the date like what apple health show in Health app.
是的,有一些解决方法,例如按日期对[HKQuantitySample]
中的数据进行手动排序.但是使用predicates
或其他方法可能可以解决.请随时询问您是否需要其他信息.
Yes there is workaround like manually sort the data from [HKQuantitySample]
by date wise. but there may be workaround by using predicates
or something else. Please feel free to ask if you need any extra information.
如@Allan所建议我添加了 HKStatisticsCollectionQuery ,是的,它按日期按日期提供数据,但是步骤计数接收与Apple health App中的计数不同.下面的代码中需要添加/修改吗?
As suggested by @AllanI have added HKStatisticsCollectionQuery, YES it gives data datewise but the steps count receiving is not same as in Apple health App. is something Addition/modification required in below code ?
let last10Day = Calendar.current.date(byAdding: .day, value: -10, to: Date())!
var interval = DateComponents()
interval.day = 1
let quantityType1 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
// Create the query
let query = HKStatisticsCollectionQuery(quantityType: quantityType1,
quantitySamplePredicate: nil,
options: .cumulativeSum,
anchorDate: last10Day,
intervalComponents: interval)
// Set the results handler
query.initialResultsHandler = {
query, results, error in
guard let statsCollection = results else {
// Perform proper error handling here
fatalError("*** An error occurred while calculating the statistics: \(String(describing: error?.localizedDescription)) ***")
}
let endDate = Date()
statsCollection.enumerateStatistics(from: last10Day, to: endDate, with: { (statistics, stop) in
if let quantity = statistics.sumQuantity() {
let date = statistics.startDate
let value = quantity.doubleValue(for: HKUnit.count())
print("--value-->",value, ",--For Date-->",date)
}
})
}
healthStore.execute(query)
推荐答案
使用HKStatisticsCollectionQuery
提取特定时间段内的数据.这是一个示例,显示了如何获取最近30天的步骤:
Use HKStatisticsCollectionQuery
to fetch data from a certain period. Here is an example showing how to fetch steps for the last 30 days:
private let healthStore = HKHealthStore()
private let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
func importStepsHistory() {
let now = Date()
let startDate = Calendar.current.date(byAdding: .day, value: -30, to: now)!
var interval = DateComponents()
interval.day = 1
var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
anchorComponents.hour = 0
let anchorDate = Calendar.current.date(from: anchorComponents)!
let query = HKStatisticsCollectionQuery(quantityType: stepsQuantityType,
quantitySamplePredicate: nil,
options: [.cumulativeSum],
anchorDate: anchorDate,
intervalComponents: interval)
query.initialResultsHandler = { _, results, error in
guard let results = results else {
log.error("Error returned form resultHandler = \(String(describing: error?.localizedDescription))")
return
}
results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
if let sum = statistics.sumQuantity() {
let steps = sum.doubleValue(for: HKUnit.count())
print("Amount of steps: \(steps), date: \(statistics.startDate)")
}
}
}
healthStore.execute(query)
}
这篇关于如何按日期明智地获取Apple健康数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!