I'm retrieving sleepAnalysis
data from HealthKit
and want to store these samples in a global array but once I'm outside the scope of the HKSampleQuery
, self.globalVariable
goes back to it's initial value of an empty array. How can I retrieve these values outside of the query scope?
if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) {
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: sleepType, predicate: nil,limit: 90, sortDescriptors: [sortDescriptor]) { (query, tmpResult, error)-> Void in
if error != nil {
return
}
if let result = tmpResult {
for item in result {
if let sample = item as? HKCategorySample {
samples.append(sample)
}
}
}
healthStore.execute(query)
}
}
Outside of the let query
scope, samples is nil.