Skip to content

Instantly share code, notes, and snippets.

@NSAmit
Last active March 18, 2019 05:36
Show Gist options
  • Save NSAmit/3a5b2e205348cdbf420bda9cfe005152 to your computer and use it in GitHub Desktop.
Save NSAmit/3a5b2e205348cdbf420bda9cfe005152 to your computer and use it in GitHub Desktop.
func getRecordForType(type: HKClinicalTypeIdentifier, recordReadCompletionHandler:@escaping ([HKClinicalRecord]?) -> Void) {
guard let healthRecordType = HKObjectType.clinicalType(forIdentifier: type) else {
fatalError("*** Unable to create the record type ***")
}
let healthRecordQuery = HKSampleQuery(sampleType: healthRecordType, predicate: nil, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, samples, error) in
guard let actualSamples = samples else {
// Handle the error here.
print("*** An error occurred: \(error?.localizedDescription ?? "nil") ***")
recordReadCompletionHandler(nil)
return
}
let healthRecordSamples = actualSamples as? [HKClinicalRecord]
recordReadCompletionHandler(healthRecordSamples)
}
// Execute the query using single HKHealthStore execute API
healthStore.execute(healthRecordQuery)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment