我正在sleepAnalysis
从中检索数据HealthKit
并希望将这些样本存储在全局数组中,但是一旦我超出了 的范围HKSampleQuery
,self.globalVariable
就会返回到它的空数组的初始值。如何在查询范围之外检索这些值?
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)
}
}
在let query
范围之外,样本为零。