Apple HealthKit的睡眠跟踪和分析

发布于 2025-02-13 23:30:00 字数 571 浏览 0 评论 0原文

我正在使用Apple HealthKit构建睡眠分析申请,并希望检索每晚的睡眠统计数据(REM,Deep,Light等)。 Apple开发人员视频提供了以下代码以在所有睡眠阶段检索样品...

let stagePredicate = HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: .allAsleepValues)

let queryPredicate = HKSamplePredicate.sample(type: HKCategoryType(.sleepAnalysis), predicate: stagePredicate)

let sleepQuery = HKSampleQueryDescriptor(predicates: [queryPredicate], sortDescriptors: [])

// Run the query
let sleepSamples = try async sleepQuery.result(for: healthStore) 


但是如何计算前一天晚上每个阶段的时间?我是HealthKit的新手,因此对任何帮助都将不胜感激。

I'm building an application for sleep analysis using Apple Healthkit and wish to retrieve nightly sleep statistics (time in REM, deep, light etc). The apple developer video gives the following code to retrieve samples in all sleep stages...

let stagePredicate = HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: .allAsleepValues)

let queryPredicate = HKSamplePredicate.sample(type: HKCategoryType(.sleepAnalysis), predicate: stagePredicate)

let sleepQuery = HKSampleQueryDescriptor(predicates: [queryPredicate], sortDescriptors: [])

// Run the query
let sleepSamples = try async sleepQuery.result(for: healthStore) 


but how do I compute the time in each of the stages for the previous night? I'm very new to healthkit so any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-02-20 23:30:01

要计算每个阶段的时间,您需要在样本上迭代并检查类别值。您可以根据要查看的样本的持续时间保留每个阶段的时间间隔,并将其递增。

棘手的部分是确定“前一天晚上”是什么。尝试定义同等尺寸的存储桶,在其中您将该存储桶中的所有样本计算为同一“睡眠会话”的一部分(例如6pm-下午6点,而不是上午12点至凌晨12点)。

To compute the time in each stage you'll want to iterate over the samples and check the category value. You can keep a time interval per stage and increment it based on the duration of the sample you're looking at.

The tricky part is determining what "previous night" is. Try defining equally sized buckets where you will count all samples in that bucket as part of the same "sleep session" (e.g. 6pm - 6pm instead of 12am - 12am).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文