Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/kingstinct/react-native-healthkit/llms.txt

Use this file to discover all available pages before exploring further.

queryCategorySamples

Query category samples from HealthKit with flexible filtering and sorting options.
queryCategorySamples<T extends CategoryTypeIdentifier>(
  identifier: T,
  options: QueryOptionsWithSortOrder
): Promise<readonly CategorySampleTyped<T>[]>

Parameters

identifier
CategoryTypeIdentifier
required
The category type identifier to query. See Category Type Identifiers for all available types.
options
QueryOptionsWithSortOrder
required
Query options for filtering and sorting the results.

Returns

samples
readonly CategorySampleTyped<T>[]
An array of category samples matching the query criteria.

Example

import HealthKit, { CategoryValueSleepAnalysis } from 'react-native-healthkit';

// Query sleep analysis samples for the last 7 days
const samples = await HealthKit.queryCategorySamples(
  'HKCategoryTypeIdentifierSleepAnalysis',
  {
    limit: 100,
    ascending: false,
    filter: {
      date: {
        startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
        endDate: new Date(),
      },
    },
  }
);

samples.forEach((sample) => {
  console.log(`Sleep stage: ${sample.value}`);
  console.log(`Duration: ${sample.endDate.getTime() - sample.startDate.getTime()}ms`);
});

queryCategorySamplesWithAnchor

Query category samples with anchor-based pagination for efficient data synchronization.
queryCategorySamplesWithAnchor<T extends CategoryTypeIdentifier>(
  identifier: T,
  options: QueryOptionsWithAnchor
): Promise<CategorySamplesWithAnchorResponseTyped<T>>

Parameters

identifier
CategoryTypeIdentifier
required
The category type identifier to query. See Category Type Identifiers for all available types.
options
QueryOptionsWithAnchor
required
Query options including anchor for pagination.

Returns

response
CategorySamplesWithAnchorResponseTyped<T>
Response object containing samples, deleted samples, and a new anchor.

Example

import HealthKit from 'react-native-healthkit';
import AsyncStorage from '@react-native-async-storage/async-storage';

// First query - no anchor
let response = await HealthKit.queryCategorySamplesWithAnchor(
  'HKCategoryTypeIdentifierMindfulSession',
  {
    limit: -1, // Get all samples
  }
);

console.log(`Fetched ${response.samples.length} mindful sessions`);

// Save the anchor for next time
await AsyncStorage.setItem('mindfulSessionAnchor', response.newAnchor);

// Later query - use stored anchor to get only new data
const savedAnchor = await AsyncStorage.getItem('mindfulSessionAnchor');

if (savedAnchor) {
  response = await HealthKit.queryCategorySamplesWithAnchor(
    'HKCategoryTypeIdentifierMindfulSession',
    {
      limit: -1,
      anchor: savedAnchor,
    }
  );
  
  console.log(`New samples: ${response.samples.length}`);
  console.log(`Deleted samples: ${response.deletedSamples.length}`);
  
  // Update the anchor
  await AsyncStorage.setItem('mindfulSessionAnchor', response.newAnchor);
}
The anchor-based query is particularly useful for:
  • Syncing data efficiently with your backend
  • Implementing real-time updates in your app
  • Reducing data transfer by fetching only changes
Always store the newAnchor value and use it in subsequent queries to avoid fetching duplicate data.