This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func quantitySampleQuery(from startDate: Date, | |
to endDate: Date?, | |
with options: HKQueryOptions = [.strictStartDate, .strictEndDate], | |
for sampleType: HKQuantityType, | |
limitingResultsTo limit: Int = Int(HKObjectQueryNoLimit), | |
using resultsHandler: @escaping QuantitySampleQueryResultsHandler) -> HKSampleQuery { | |
// Build the query | |
let query = baseSampleQuery(from: startDate, to: endDate, with: options, for: sampleType, limitingResultsTo: limit) { (query, result) in | |
switch result { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func baseSampleQuery(from startDate: Date, | |
to endDate: Date?, | |
with options: HKQueryOptions = [.strictStartDate, .strictEndDate], | |
for sampleType: HKQuantityType, | |
limitingResultsTo limit: Int = Int(HKObjectQueryNoLimit), | |
using resultsHandler: @escaping BaseSampleQueryResultsHandler) -> HKSampleQuery { | |
// Build sort descriptor to return the samples in descending order | |
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Result<Value, Error> { | |
case success(Value) | |
case failure(Error) | |
} | |
public enum HealthUpQueryError: Error { | |
case noSamplesReturned | |
case undefinedError | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public typealias QuantitySampleQueryResultsHandler = (_ query: HKSampleQuery, _ samples: [HKQuantitySample]) -> Void | |
public typealias CategorySampleQueryResultsHandler = (_ query: HKSampleQuery, _ samples: [HKCategorySample]) -> Void | |
func quantitySampleQuery(startDate: Date, | |
endDate: Date?, | |
dateOptions: HKQueryOptions = [.strictStartDate, .strictEndDate], | |
sampleType: HKQuantityType, | |
resultsLimit: Int = Int(HKObjectQueryNoLimit), | |
resultsHandler: @escaping QuantitySampleQueryResultsHandler) -> HKSampleQuery { | |