HealthKit Code Review
Quick Reference
| Issue Type | Reference |
|---|
| HKHealthStore, permissions, status checks, privacy | references/authorization.md |
| HKQuery types, predicates, anchored queries, statistics |
references/queries.md |
| Background delivery, observer queries, completion handlers |
references/background.md |
| HKQuantityType, HKCategoryType, workouts, units |
references/data-types.md |
Review Checklist
- - [ ]
HKHealthStore.isHealthDataAvailable() called before any HealthKit operations - [ ] Authorization requested only for needed data types (minimal permissions)
- [ ]
requestAuthorization completion handler not misinterpreted as permission granted - [ ] No attempt to determine read permission status (privacy by design)
- [ ] Query results dispatched to main thread for UI updates
- [ ]
HKObjectQueryNoLimit used only with bounded predicates - [ ]
HKStatisticsQuery used for aggregations instead of manual summing - [ ] Observer query
completionHandler() always called (use defer) - [ ] Background delivery registered in INLINECODE6
- [ ] Background delivery entitlement added (iOS 15+)
- [ ] Correct units used for quantity types (e.g.,
count/min for heart rate) - [ ] Long-running queries stored as properties and stopped in INLINECODE8
When to Load References
- - Reviewing authorization/permissions flow -> authorization.md
- Reviewing HKSampleQuery, HKAnchoredObjectQuery, or predicates -> queries.md
- Reviewing HKObserverQuery or
enableBackgroundDelivery -> background.md - Reviewing HKQuantityType, HKCategoryType, or HKWorkout -> data-types.md
Review Questions
- 1. Is
isHealthDataAvailable() checked before creating HKHealthStore? - Does the code gracefully handle denied permissions (empty results)?
- Are observer query completion handlers called in all code paths?
- Is work in background handlers minimal (~15 second limit)?
- Are HKQueryAnchors persisted per sample type (not shared)?
HealthKit 代码审查
快速参考
references/queries.md |
| 后台投递、观察者查询、完成处理器 |
references/background.md |
| HKQuantityType、HKCategoryType、锻炼、单位 |
references/data-types.md |
审查清单
- - [ ] 在任何 HealthKit 操作前调用 HKHealthStore.isHealthDataAvailable()
- [ ] 仅针对所需数据类型请求授权(最小权限原则)
- [ ] 不将 requestAuthorization 完成处理器误解为已授予权限
- [ ] 不尝试确定读取权限状态(隐私设计原则)
- [ ] 查询结果分发到主线程进行 UI 更新
- [ ] 仅在有限谓词下使用 HKObjectQueryNoLimit
- [ ] 使用 HKStatisticsQuery 进行聚合,而非手动求和
- [ ] 观察者查询的 completionHandler() 始终被调用(使用 defer)
- [ ] 在 application(_:didFinishLaunchingWithOptions:) 中注册后台投递
- [ ] 添加后台投递授权(iOS 15+)
- [ ] 为数量类型使用正确的单位(例如心率使用 count/min)
- [ ] 长时间运行的查询作为属性存储,并在 deinit 中停止
何时加载参考文档
- - 审查授权/权限流程 -> authorization.md
- 审查 HKSampleQuery、HKAnchoredObjectQuery 或谓词 -> queries.md
- 审查 HKObserverQuery 或 enableBackgroundDelivery -> background.md
- 审查 HKQuantityType、HKCategoryType 或 HKWorkout -> data-types.md
审查问题
- 1. 创建 HKHealthStore 前是否检查了 isHealthDataAvailable()?
- 代码是否优雅处理了被拒绝的权限(空结果)?
- 观察者查询的完成处理器是否在所有代码路径中被调用?
- 后台处理器中的工作是否最小化(约15秒限制)?
- HKQueryAnchors 是否按样本类型持久化(而非共享)?