Check out https://developer.apple.com/documentation/healthkit/hkobject/1615483-sourcerevision
Post
Replies
Boosts
Views
Activity
Do any of the samples overlap? If so you'll be counting too much. Are any of the samples logged as asleepUnspecified? If so, you've forgotten to count them. It's also not clear what date you're passing in to start looking back from. You're just fetching the last 24 hours but the dates in the Health app are based from 6pm-6pm.
Hello, if your device contains overlapping in bed intervals from multiple source then that's what the query will return. It will be up to you to decide how you want to handle it. You could ignore certain sources if you want, for example. Or you can keep track of the intervals yourselves in some kind of data structure.
Please confirm that that you and your friend are using the same version of watchOS and are both using the native Apple sleep tracking experience rather than a third party app.
"If a user sleeps with their Apple Watch at night with a sleep schedule set up, are CORE, REM and Deep enough to check for asleep status? Or is inBed also necessary?"
It depends on the needs of your app. If you're developing an app that is trying to calculate time asleep separate from time in bed you would not want to fetch the inBed samples from HealthKit and just check the asleep samples.
No that's available through HealthKit. You can set up shortcuts that trigger at bedtime/wakeup/etc however.
You can use allAsleepValues in your query: https://developer.apple.com/documentation/healthkit/hkcategoryvaluesleepanalysis/3931475-allasleepvalues
HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: .allAsleepValues)
Sleep is a publicly readable and writable data type so it depends on the implementation of a given application that writes data. The built in sleep app writes Sleep data at the end of the night as you've seen. Other apps may choose to behave differently depending on their sleep algorithm implementations. I hope that answers your question.
Hello,
From the look at your query it seems like you're fetching all REM samples in the health database where in your screenshot it appears you're looking at just one day of data. Perhaps that's the discrepancy you're seeing?
Thanks
I'm not sure exactly what you mean by "merging data for consecutive days" but I suspect you're querying for "what is the total amount of sleep time in this calendar day" which will include data from both the night you care about but also the morning corresponding to the night before and that's what you mean by merged. You'll likely want to query a little differently. For example, instead of bucketing by a midnight to midnight day, bucket by a 6pm - 6pm bounded day. Or apply some other type of processing to determine which "actual" day sleep data belongs to.