Apple Health, Health Connect & Samsung Health

Once a user connects Apple Health, Health Connect, or Samsung Health, the Thryve SDK can retrieve and sync data from these native sources either automatically or manually.

We generally recommend manually synchronizing connected native data sources whenever your app comes to the foreground to ensure all data is up-to-date, as automatic background data synchronization is highly dependent on the operating system and its resource and battery management, allowing for regular synchronizations.

circle-info

Web data sources are seamlessly managed by the Thryve backend. This page is only related to data sources where health data is directly retrieved from the end user device via the Thryve SDK.

Automatic Background Data Synchronization

We utilize different technologies for automatic background data retrieval on iOS and Android. On iOS, the Thryve SDK employs HealthKit’s native background synchronization API. For Android, synchronization is scheduled and managed via WorkManagerarrow-up-right.

The availability of background data synchronizations is configurable for both Apple Health and Health Connect data sources in the corresponding config objects. Samsung Health automatically synchronizes data in the background once the user successfully connects.

To enable background data synchronization (retrieving data when your app is closed or idle), use the executeBackgroundSync method within the didFinishLaunchingWithOptions function in AppDelegate right after connecting to Apple Health.

Apple mandates that this logic runs within the first few seconds of app startup for data delivery, hence its placement in AppDelegate. Refer to the sample code for further details.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // When Apple's Health app initiates contact, it triggers your application 
        // via didFinishLaunchingWithOptions. Once activated, HealthKit waits 
        // briefly to determine if your app wishes to renew its query contracts. 
        // Calling the relevant method as early as possible renews these contracts, 
        // enabling the app to fetch new data promptly from Apple Health.
        ThryveSDK.get().executeBackgroundSync(
                  dataSource: .apple,
                  dataTypes: ThryveAppleHealthDataType.allTypes)
        // Override point for customization after application launch.
        return true
    }
triangle-exclamation

Manual Data Synchronization

Manual data synchronization is recommended to ensure all recent data is available when the user opens your app. Therefore, we recommend using synchronize whenever your app moves to the foreground.

synchronize will ensure that both epoch and daily data added since the last data synchronization will be fetched from Apple Health, Health Connect, or Samsung Health. The initial execution of synchronize will fetch all data for today.

circle-info

The manual data retrieval methods will return a ThryveResponse. This response will be true if the data is uploaded successfully. If issues occur during retrieval or upload, the response will be false, and a list of ThryveError will be provided. Each ThryveError contains details about the error encountered.

Historic data backfill

To access data predating the initial synchronization, the Thryve SDK provides methods for backfilling historical data. These methods allow synchronization of epoch and daily data within a specified timeframe.

circle-info

backfillEpoch fetches epoch data for the given timeframe, with a maximum of 30 days. If the difference between startDate and endDate is more than 30 days, the SDK will count from the end date 30 days back

backfillDaily fetches daily data for the given timeframe, with a maximum of 365 days. If the difference between startDate and endDate is more than 365 days, the SDK will count from the end date 365 days back

Parameter
Description
Mandatory

dataSource

The data source to synchronize data from. iOS defaults to .apple

yes, on Android. no, on iOS

dataTypes

The data types to synchronize. It defaults to the data types defined in the data source config.

no

startDate

The date to start data retrieval and synchronization from. It is applicable only to the backfiill functions

yes

endDate

The date to end data retrieval and synchronization on. It defaults to today and applicable only to the backfiill functions.

no

Last updated