From 3ffe295e6bd271b0a31026e59fc82ccaebbbba83 Mon Sep 17 00:00:00 2001 From: Josh Parnham Date: Mon, 25 Sep 2023 15:49:46 -0700 Subject: [PATCH] Update queryStatisticsForQuantity to resolve when statistics in callback is nil --- ios/ReactNativeHealthkit.swift | 65 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/ios/ReactNativeHealthkit.swift b/ios/ReactNativeHealthkit.swift index 06f436a..eba7026 100644 --- a/ios/ReactNativeHealthkit.swift +++ b/ios/ReactNativeHealthkit.swift @@ -673,42 +673,45 @@ class ReactNativeHealthkit: RCTEventEmitter { } let query = HKStatisticsQuery.init(quantityType: quantityType, quantitySamplePredicate: predicate, options: opts) { (_, stats: HKStatistics?, _: Error?) in - if let gottenStats = stats { - var dic = [String: [String: Any]?]() - let unit = HKUnit.init(from: unitString) - if let averageQuantity = gottenStats.averageQuantity() { - dic.updateValue(serializeQuantity(unit: unit, quantity: averageQuantity), forKey: "averageQuantity") - } - if let maximumQuantity = gottenStats.maximumQuantity() { - dic.updateValue(serializeQuantity(unit: unit, quantity: maximumQuantity), forKey: "maximumQuantity") - } - if let minimumQuantity = gottenStats.minimumQuantity() { - dic.updateValue(serializeQuantity(unit: unit, quantity: minimumQuantity), forKey: "minimumQuantity") - } - if let sumQuantity = gottenStats.sumQuantity() { - dic.updateValue(serializeQuantity(unit: unit, quantity: sumQuantity), forKey: "sumQuantity") + var dic = [String: [String: Any]?]() + + guard let gottenStats = stats else { + return resolve(dic) + } + + let unit = HKUnit.init(from: unitString) + if let averageQuantity = gottenStats.averageQuantity() { + dic.updateValue(serializeQuantity(unit: unit, quantity: averageQuantity), forKey: "averageQuantity") + } + if let maximumQuantity = gottenStats.maximumQuantity() { + dic.updateValue(serializeQuantity(unit: unit, quantity: maximumQuantity), forKey: "maximumQuantity") + } + if let minimumQuantity = gottenStats.minimumQuantity() { + dic.updateValue(serializeQuantity(unit: unit, quantity: minimumQuantity), forKey: "minimumQuantity") + } + if let sumQuantity = gottenStats.sumQuantity() { + dic.updateValue(serializeQuantity(unit: unit, quantity: sumQuantity), forKey: "sumQuantity") + } + if #available(iOS 12, *) { + if let mostRecent = gottenStats.mostRecentQuantity() { + dic.updateValue(serializeQuantity(unit: unit, quantity: mostRecent), forKey: "mostRecentQuantity") } - if #available(iOS 12, *) { - if let mostRecent = gottenStats.mostRecentQuantity() { - dic.updateValue(serializeQuantity(unit: unit, quantity: mostRecent), forKey: "mostRecentQuantity") - } - if let mostRecentDateInterval = gottenStats.mostRecentQuantityDateInterval() { - dic.updateValue([ - "start": self._dateFormatter.string(from: mostRecentDateInterval.start), - "end": self._dateFormatter.string(from: mostRecentDateInterval.end) - ], forKey: "mostRecentQuantityDateInterval") - } + if let mostRecentDateInterval = gottenStats.mostRecentQuantityDateInterval() { + dic.updateValue([ + "start": self._dateFormatter.string(from: mostRecentDateInterval.start), + "end": self._dateFormatter.string(from: mostRecentDateInterval.end) + ], forKey: "mostRecentQuantityDateInterval") } - if #available(iOS 13, *) { - let durationUnit = HKUnit.second() - if let duration = gottenStats.duration() { - dic.updateValue(serializeQuantity(unit: durationUnit, quantity: duration), forKey: "duration") - } + } + if #available(iOS 13, *) { + let durationUnit = HKUnit.second() + if let duration = gottenStats.duration() { + dic.updateValue(serializeQuantity(unit: durationUnit, quantity: duration), forKey: "duration") } - - resolve(dic) } + + resolve(dic) } store.execute(query)