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>!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions