Last active
May 29, 2018 02:53
-
-
Save vichudson1/ba08bc756cc97d09f54436d33b9793b4 to your computer and use it in GitHub Desktop.
Code Snippet for http://vichudson1.micro.blog/2018/05/26/health-up-diaries.html
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 { | |
case .success(let samples): | |
guard let samples = samples as? [HKQuantitySample], samples.count > 0 else { | |
return resultsHandler(query, .failure(HealthUpQueryError.noSamplesReturned)) | |
} | |
resultsHandler(query, .success(samples)) | |
case .failure(let error): | |
resultsHandler(query, .failure(error)) | |
} | |
} | |
return query | |
} | |
func categorySampleQuery(from startDate: Date, | |
to endDate: Date?, | |
with options: HKQueryOptions = [.strictStartDate, .strictEndDate], | |
for sampleType: HKQuantityType, | |
limitingResultsTo limit: Int = Int(HKObjectQueryNoLimit), | |
using resultsHandler: @escaping CategorySampleQueryResultsHandler) -> HKSampleQuery { | |
// Build the query | |
let query = baseSampleQuery(from: startDate, to: endDate, with: options, for: sampleType, limitingResultsTo: limit) { (query, result) in | |
switch result { | |
case .success(let samples): | |
guard let samples = samples as? [HKCategorySample], samples.count > 0 else { | |
return resultsHandler(query, .failure(HealthUpQueryError.noSamplesReturned)) | |
} | |
resultsHandler(query, .success(samples)) | |
case .failure(let error): | |
resultsHandler(query, .failure(error)) | |
} | |
} | |
return query | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment