Differential changes API

This API enables developers to retrieve changes made to Health Connect by deploying a changes token.

Integrating with the differential changes API

Use the following example to integrate the Differential Changes API in Health Connect. Changes are retrieved from Health Connect only when your app requests them.

  1. First, obtain an initial changes token. This changes token represents the point in commit history from which changes will be taken.
suspend fun getChangesToken(client: HealthConnectClient,
                            recordTypes: Set<KClass<out Record>>) {
  val response =
   client.getChangesToken(
     ChangesTokenRequest(
       recordTypes = recordTypes,
     )
   )
}

Note that the changes token is only valid for 30 days. Ensure the following:

  1. Your app is updating from changes much more regularly than this to avoid stale tokens.
  2. Your app can handle the case where the token is no longer valid, and has a fallback mechanism for obtaining the necessary data.
  3. Request changes after obtaining the changes token:
suspend fun getChanges(client: HealthConnectClient,
                              changesToken: String) {
 val response = client.getChanges(changesToken)
 response.changes.forEach {
   // Process each change
 }
}