Skip to content

Instantly share code, notes, and snippets.

@vichudson1
Last active May 29, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vichudson1/ba08bc756cc97d09f54436d33b9793b4 to your computer and use it in GitHub Desktop.
Save vichudson1/ba08bc756cc97d09f54436d33b9793b4 to your computer and use it in GitHub Desktop.
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