Skip to content

fix: use proper units in queryStatisticsForQuantity to prevent crashes #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2025

Conversation

robertherber
Copy link
Member

Summary

  • Replace hardcoded "count" unit with getUnitToUse() in queryStatisticsForQuantity and queryStatisticsCollectionForQuantity
  • This gets the correct preferred unit for each quantity type, preventing crashes
  • Fixes issue where HKQuantityTypeIdentifierDistanceWalkingRunning and HKQuantityTypeIdentifierActiveEnergyBurned caused app crashes

Root Cause

The functions were defaulting to "count" unit regardless of quantity type:

  • HKQuantityTypeIdentifierStepCount uses "count" → worked fine
  • HKQuantityTypeIdentifierDistanceWalkingRunning needs distance units (m, km, mi) → crashed
  • HKQuantityTypeIdentifierActiveEnergyBurned needs energy units (kcal, kJ) → crashed

Solution

Use the same approach as queryQuantitySamples which already works correctly by calling getUnitToUse() to get the appropriate unit for each quantity type.

Test plan

  • Test queryStatisticsForQuantity with HKQuantityTypeIdentifierDistanceWalkingRunning - should no longer crash
  • Test queryStatisticsForQuantity with HKQuantityTypeIdentifierActiveEnergyBurned - should no longer crash
  • Test queryStatisticsForQuantity with HKQuantityTypeIdentifierStepCount - should continue working
  • Test queryStatisticsCollectionForQuantity with various quantity types - should work correctly
  • Verify that when no unit is specified, the proper preferred unit is used automatically
  • Verify that when a unit is explicitly provided, it still works correctly

Fixes #188

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Replace hardcoded "count" unit with getUnitToUse() to get the correct
preferred unit for each quantity type. This fixes crashes when using
queryStatisticsForQuantity with HKQuantityTypeIdentifierDistanceWalkingRunning
and HKQuantityTypeIdentifierActiveEnergyBurned.

Fixes #188

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

changeset-bot bot commented Jul 19, 2025

🦋 Changeset detected

Latest commit: 70cce7b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kingstinct/react-native-healthkit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Jul 19, 2025

Open in StackBlitz

npm i https://pkg.pr.new/kingstinct/react-native-healthkit/@kingstinct/react-native-healthkit@189

commit: 70cce7b

Copy link
Contributor

github-actions bot commented Jul 19, 2025

🚀 Expo continuous deployment is ready!

  • Project → react-native-healthkit-example
  • Platform → ios
  • Scheme → reactnativehealthkitexample
  🤖 Android 🍎 iOS
Fingerprint n/a 2404f49180374fc607d60ea90f5cf95ed2d89efc
Build Details n/a Build Permalink
DetailsDistribution: STORE
Build profile: development-appstore-connect
Runtime version: 2404f49180374fc607d60ea90f5cf95ed2d89efc
App version: 1.0.0
Git commit: a00cad3014d771c23de9977fcadfcd7522bdd884
Update Details n/a Update Permalink
DetailsBranch: 189/merge
Runtime version: b7aae01516fdec4308234822cd7fbe2f66bdec51
Git commit: a00cad3014d771c23de9977fcadfcd7522bdd884
Update QR n/a

@robertherber robertherber merged commit 3fa7529 into master Jul 19, 2025
9 checks passed
@robertherber robertherber deleted the conductor/quebec branch July 19, 2025 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"HKQuantityTypeIdentifierDistanceWalkingRunning" using queryStatisticsForQuantity causes crash
1 participant