Slide 15
Slide 15 text
import HealthKit
// ൣғͷϑΟϧλΛ࡞
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate,
options: .strictStartDate)
// αϯϓϧPredicateΛ࡞
let queryPredicate = HKSamplePredicate.sample(type:
HKCategoryType(.sleepAnalysis), predicate: predicate)
// ΫΤϦσΟεΫϦϓλΛ࡞
let descriptor = HKSampleQueryDescriptor(
predicates: [queryPredicate],
// ։࢝࣌ (startDate) Λঢॱ (ݹ͍ॱ) ʹฒସ͑
sortDescriptors: [SortDescriptor(\.startDate, order: .forward)],
limit: HKObjectQueryNoLimit // શͯͷαϯϓϧΛऔಘ
)
let healthStore = HKHealthStore()
do {
// ඇಉظͰσʔλΛऔಘ
let samples: [HKSample] = try await descriptor.result(for: healthStore)
} catch {
// Τϥʔॲཧ
}
ਭऔಘ