Last active
May 29, 2018 02:47
-
-
Save vichudson1/d16e65fe9384700321874563aecd14ca to your computer and use it in GitHub Desktop.
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) | |
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