Skip to content

Instantly share code, notes, and snippets.

@vichudson1
Last active May 29, 2018 02:47
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/d16e65fe9384700321874563aecd14ca to your computer and use it in GitHub Desktop.
Save vichudson1/d16e65fe9384700321874563aecd14ca to your computer and use it in GitHub Desktop.
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)
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: options)
// Build the query
let query = HKSampleQuery(sampleType: sampleType, predicate: datePredicate, limit: limit, sortDescriptors: [sortDescriptor]) {
(sampleQuery, results, error ) -> Void in
guard error == nil, let results = results else {
guard let error = error else { return resultsHandler(sampleQuery, BaseSampleResult.failure(HealthUpQueryError.undefinedError)) }
return resultsHandler(sampleQuery, BaseSampleResult.failure(error))
}
resultsHandler(sampleQuery, .success(results))
}
return query
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment