Query quantity samples using HealthKit anchors for efficient syncing. This method is ideal for keeping your app’s data in sync with HealthKit, as it returns only the samples that have changed since the last query.
import { queryQuantitySamplesWithAnchor } from '@kingstinct/react-native-healthkit'// First sync - no anchorconst { newAnchor, samples, deletedSamples } = await queryQuantitySamplesWithAnchor( 'HKQuantityTypeIdentifierStepCount', { limit: 100 })console.log(`Initial sync: ${samples.length} samples`)// Store the anchor for next timeconst storedAnchor = newAnchor// Later, sync only changesconst nextResult = await queryQuantitySamplesWithAnchor( 'HKQuantityTypeIdentifierStepCount', { limit: 100, anchor: storedAnchor })console.log(`New samples: ${nextResult.samples.length}`)console.log(`Deleted samples: ${nextResult.deletedSamples.length}`)
Always store the newAnchor value after each successful sync. Use this anchor in subsequent queries to retrieve only the changes since the last sync.
Setting limit: 0 will return all available samples, but for large datasets, it’s recommended to use a reasonable limit and paginate through results using the anchor.