HealthKit sleep analysis

Until the last Apple Watch update I was retrieving sleep data from HealthKit using the .asleep identifier and reliable got sleep and the date the sleep was credited to in Apple Health. Since the update there appears to be no reliable way to make sure that the sleep calculated from the various sleep states get credited to the same day Apple Health credits the sleep to.

We have no interest in analyzing sleep states, all we want is total sleep for a given day and that the date for the sleep is the same as Apple Health displays.

How can this be reliably achieved?

You can use allAsleepValues in your query: https://developer.apple.com/documentation/healthkit/hkcategoryvaluesleepanalysis/3931475-allasleepvalues

HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: .allAsleepValues)

HealthKit sleep analysis
 
 
Q