Skip to content

Instantly share code, notes, and snippets.

View vichudson1's full-sized avatar
🏠
Working from home

Vic Hudson vichudson1

🏠
Working from home
View GitHub Profile
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 {
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)
public enum Result<Value, Error> {
case success(Value)
case failure(Error)
}
public enum HealthUpQueryError: Error {
case noSamplesReturned
case undefinedError
}
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 {