feat: 修复健康数据
This commit is contained in:
@@ -326,22 +326,20 @@ class HealthKitManager: NSObject, RCTBridgeModule {
|
||||
endDate = Date()
|
||||
}
|
||||
|
||||
// 使用 HKStatisticsQuery 代替 HKSampleQuery 来直接获取总和,避免处理大量样本
|
||||
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
|
||||
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
|
||||
|
||||
let query = HKSampleQuery(sampleType: basalEnergyType,
|
||||
predicate: predicate,
|
||||
limit: HKObjectQueryNoLimit,
|
||||
sortDescriptors: [sortDescriptor]) { [weak self] (query, samples, error) in
|
||||
let query = HKStatisticsQuery(quantityType: basalEnergyType,
|
||||
quantitySamplePredicate: predicate,
|
||||
options: .cumulativeSum) { [weak self] (query, statistics, error) in
|
||||
DispatchQueue.main.async {
|
||||
if let error = error {
|
||||
rejecter("QUERY_ERROR", "Failed to query basal energy: \(error.localizedDescription)", error)
|
||||
return
|
||||
}
|
||||
|
||||
guard let energySamples = samples as? [HKQuantitySample] else {
|
||||
guard let statistics = statistics else {
|
||||
resolver([
|
||||
"data": [],
|
||||
"totalValue": 0,
|
||||
"startDate": self?.dateToISOString(startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(endDate) ?? ""
|
||||
@@ -349,28 +347,10 @@ class HealthKitManager: NSObject, RCTBridgeModule {
|
||||
return
|
||||
}
|
||||
|
||||
let energyData = energySamples.map { sample in
|
||||
[
|
||||
"id": sample.uuid.uuidString,
|
||||
"startDate": self?.dateToISOString(sample.startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(sample.endDate) ?? "",
|
||||
"value": sample.quantity.doubleValue(for: HKUnit.kilocalorie()),
|
||||
"source": [
|
||||
"name": sample.sourceRevision.source.name,
|
||||
"bundleIdentifier": sample.sourceRevision.source.bundleIdentifier
|
||||
],
|
||||
"metadata": sample.metadata ?? [:]
|
||||
] as [String : Any]
|
||||
}
|
||||
|
||||
let totalValue = energySamples.reduce(0.0) { total, sample in
|
||||
return total + sample.quantity.doubleValue(for: HKUnit.kilocalorie())
|
||||
}
|
||||
let totalValue = statistics.sumQuantity()?.doubleValue(for: HKUnit.kilocalorie()) ?? 0
|
||||
|
||||
let result: [String: Any] = [
|
||||
"data": energyData,
|
||||
"totalValue": totalValue,
|
||||
"count": energyData.count,
|
||||
"startDate": self?.dateToISOString(startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(endDate) ?? ""
|
||||
]
|
||||
@@ -872,22 +852,20 @@ class HealthKitManager: NSObject, RCTBridgeModule {
|
||||
endDate = Date()
|
||||
}
|
||||
|
||||
// 使用 HKStatisticsQuery 代替 HKSampleQuery 来直接获取步数总和,提高性能
|
||||
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
|
||||
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
|
||||
|
||||
let query = HKSampleQuery(sampleType: stepType,
|
||||
predicate: predicate,
|
||||
limit: HKObjectQueryNoLimit,
|
||||
sortDescriptors: [sortDescriptor]) { [weak self] (query, samples, error) in
|
||||
let query = HKStatisticsQuery(quantityType: stepType,
|
||||
quantitySamplePredicate: predicate,
|
||||
options: .cumulativeSum) { [weak self] (query, statistics, error) in
|
||||
DispatchQueue.main.async {
|
||||
if let error = error {
|
||||
rejecter("QUERY_ERROR", "Failed to query step count: \(error.localizedDescription)", error)
|
||||
return
|
||||
}
|
||||
|
||||
guard let stepSamples = samples as? [HKQuantitySample] else {
|
||||
guard let statistics = statistics else {
|
||||
resolver([
|
||||
"data": [],
|
||||
"totalValue": 0,
|
||||
"startDate": self?.dateToISOString(startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(endDate) ?? ""
|
||||
@@ -895,28 +873,10 @@ class HealthKitManager: NSObject, RCTBridgeModule {
|
||||
return
|
||||
}
|
||||
|
||||
let stepData = stepSamples.map { sample in
|
||||
[
|
||||
"id": sample.uuid.uuidString,
|
||||
"startDate": self?.dateToISOString(sample.startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(sample.endDate) ?? "",
|
||||
"value": sample.quantity.doubleValue(for: HKUnit.count()),
|
||||
"source": [
|
||||
"name": sample.sourceRevision.source.name,
|
||||
"bundleIdentifier": sample.sourceRevision.source.bundleIdentifier
|
||||
],
|
||||
"metadata": sample.metadata ?? [:]
|
||||
] as [String : Any]
|
||||
}
|
||||
|
||||
let totalValue = stepSamples.reduce(0.0) { total, sample in
|
||||
return total + sample.quantity.doubleValue(for: HKUnit.count())
|
||||
}
|
||||
let totalValue = statistics.sumQuantity()?.doubleValue(for: HKUnit.count()) ?? 0
|
||||
|
||||
let result: [String: Any] = [
|
||||
"data": stepData,
|
||||
"totalValue": totalValue,
|
||||
"count": stepData.count,
|
||||
"startDate": self?.dateToISOString(startDate) ?? "",
|
||||
"endDate": self?.dateToISOString(endDate) ?? ""
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user