Last active
May 29, 2018 02:40
-
-
Save vichudson1/5af2036966cfd0dabba8b0f469e8e568 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
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 { | |
// Build sort descriptor to return the samples in descending order | |
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false) | |
// Limit the number of samples returned by the query to the number requested | |
let limit = resultsLimit | |
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: dateOptions) | |
// Build the query | |
let query = HKSampleQuery(sampleType: sampleType, predicate: datePredicate, limit: limit, sortDescriptors: [sortDescriptor]) { | |
(sampleQuery, results, error ) -> Void in | |
guard let samples = results as? [HKQuantitySample] , error == nil else { | |
resultsHandler(sampleQuery, []) | |
guard let queryError = error else { return } | |
print("\(queryError.localizedDescription)") | |
return | |
} | |
resultsHandler(sampleQuery, samples) | |
} | |
return query | |
} | |
func categorySampleQuery(startDate: Date, | |
endDate: Date, | |
sampleType: HKCategoryType, | |
resultsLimit: Int = Int(HKObjectQueryNoLimit), | |
resultsHandler: @escaping CategorySampleQueryResultsHandler) -> HKSampleQuery { | |
// Build sort descriptor to return the samples in descending order | |
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false) | |
// Limit the number of samples returned by the query to the number requested | |
let limit = resultsLimit | |
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate, .strictEndDate]) | |
// Build the query | |
let query = HKSampleQuery(sampleType: sampleType, predicate: datePredicate, limit: limit, sortDescriptors: [sortDescriptor]) { | |
(sampleQuery, results, error ) -> Void in | |
guard let samples = results as? [HKCategorySample] , error == nil else { | |
resultsHandler(sampleQuery, []) | |
guard let queryError = error else { return } | |
print("\(queryError.localizedDescription)") | |
return | |
} | |
resultsHandler(sampleQuery, samples) | |
} | |
return query | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment