This is the copy of source code one could see on What's new in Health Kit Session:
// predicate for sleep samples
let remSleepStagePredicate = HKCategoryValueSleepAnalysis.predicateForSamples(.equalTo, value: .asleepREM)
// create query with the designated sleepStagePredicate
let queryPredicate = HKSamplePredicate.sample(type: HKCategoryType(.sleepAnalysis), predicate: remSleepStagePredicate)
// Sleep Query
let sleepQuery = HKSampleQueryDescriptor(predicates: [queryPredicate], sortDescriptors: [])
// Run the query
let sleepSamples = try async sleepQuery.result(for: healthStore)
I tried to take a snaphot of the session itself, but for some reason I could not submit it with the file attached so I provided the code above.
I am wondering that shouldn't it be try await
, not try async
on the last line of the source code?
so the last line should be the code below:
let sleepSamples = try await sleepQuery.result(for: healthStore)
Am I mistaken, or is it a human error?
It depends on what you want to do.
With await, you will not proceed until it has compelted.
With async, you move on, working in parallel while the query completes.
See details here : https://www.hackingwithswift.com/quick-start/concurrency/whats-the-difference-between-await-and-async-let