Skip to content

Duplicate step counts from iPhone + Apple Watch #301

Description

@R0LLeX

@kingstinct/react-native-healthkit@13.1.1

Hi!
I’m using queryStatisticsForQuantity to get today’s step count from HealthKit.

export const fetchTodaySteps = async (): Promise<number> => {
    const res = await queryStatisticsForQuantity('HKQuantityTypeIdentifierStepCount', ['cumulativeSum'], {
        filter: {
            date: {
                startDate: startOfDay(new Date()),
                endDate: endOfDay(new Date()),
            },
            metadata: { operatorType: 5, withMetadataKey: 'HKWasUserEntered', value: true },
        },
    })

    return res.sumQuantity?.quantity || 0
}

When a user has both iPhone and Apple Watch, HealthKit contains step samples from multiple sources. As a result, queryStatisticsForQuantity seems to sum steps from both devices, which leads to duplicated counts.

Example:

Real steps today: ~6000

Returned by queryStatisticsForQuantity: ~10000
(because steps from iPhone + Apple Watch are both included)

According to Apple HealthKit docs, step data should usually be queried in a way that respects Health’s aggregation logic (preferred sources, statistics queries, etc.), so that the final value represents the actual total — not a sum of raw samples from all devices.

Question:
Is there a recommended way (or an existing API in this library) to fetch deduplicated / aggregated step count from HealthKit without manually filtering samples by source?

Thanks!

UPDATE:
I also found another method in the library:
queryStatisticsForQuantitySeparateBySource.

I tried using it to get step statistics separated by source, but when calling this method I get the following error:
Error: Cannot convert nullptr to NativeState<margelo::nitro::healthkit::HybridSourceProxySpec>!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions