HealthKit total sleep time

Hi, I have a problem with HealthKit. When fetching sleepAnalysis, instead of getting a record for every night, I get multiple records for each night. How can I calculate the total amount of time for every day from the startDate and the endDate of every record? Thanks.

Replies

There isn't a direct HealthKit query to get that answer. You'd need to calculate total sleep time by adding the duration of the individual samples and accounting for any overlaps. You should also distinguish between InBed and Asleep samples since they will certainly overlap.

What is the logic used in Health kit to show sleep data weekly on graph in Six Month segment?