SDK Synchronization
What the SDK sends, when it sends it, and when the data shows up in the API.
This page describes the iOS SDK, which the React Native module wraps. The Android SDK syncs the same way, with Health Connect in place of Apple Health.
The SDK only reads. It never writes to Apple Health. Your app does not schedule uploads or call an upload endpoint: the SDK sends the data straight to Sonar, not through your backend.
What It Reads
At connect, the SDK asks for read access to every type in Supported Native Data. It syncs what the user allowed and what exists on the phone. Apple does not tell apps which types were refused, so a refused type looks the same as a type with no data. If the user allows more types later in Settings, call resync(provider) so their history is read too.
The First Import
Recent Days
Right after connect, the SDK sends the last 30 days. Sonar processes them at once and sends your backend its first subject.synced webhook.
History
The SDK then works back to the start of the history period: two years before the connect date, unless you chose another History Period.
New Data
From then on the SDK sends new data as the phone records it. New data always goes before the rest of the history.
backfill in the provider state shows how far the import has come. See SDK Interface.
History Period
historyDays in configure sets how far back a new connection imports, recent days included. It goes from 30 (one month) to 1825 (five years), and the default is 730 (two years). A value outside that range fails with not_configured.
try Sonar.configure(appId: "app_Qm3t8Vx1LmN0aB7cD9eF2g", historyDays: 365)A connection keeps the period it started with. A new value applies to users who connect after it, and to a connected user when you call resync(provider). With 30 days, the import ends after the recent days.
When It Syncs
The SDK syncs when the app comes to the foreground, when you call sync(), and in the background. For background work it uses HealthKit background delivery and iOS background tasks, so your app must enable both. See iOS (Swift) for the setup.
iOS decides when an app may run in the background. The SDK cannot promise when a background sync happens. Syncing is fastest while your app is open.
A locked phone syncs later
iOS hides Health data while the phone is locked. Data recorded then arrives after the next unlock. The state shows idle, not an error. A phone that stays locked and unused sends nothing.
When there is nothing new to send, the SDK still checks in with Sonar from time to time. That keeps the connection’s last_seen_at current, so a stale connection means the phone stopped running the SDK, not that it had nothing to send. See What Your Backend Sees.
A sync that is cut short loses nothing. The next one continues where it stopped.
Source Apps
After a run, the SDK also tells Sonar which apps on the phone hold sleep and workout data — on iOS from HealthKit’s source list, on Android from the sessions Health Connect keeps. It sends the list when it changes, and at least once a day. Sonar keeps the latest list only, so an app whose data is gone from the phone leaves it.
Edits and Deletions
When the user edits data from about the last week in Apple Health, the change reaches Sonar on its own and replaces what Sonar holds. Edits to older data reach Sonar only through resync(provider).
Deleted records
When a user deletes a record in Apple Health, Sonar keeps it. Daily totals such as steps still follow a recent deletion, because the SDK sends recent totals again. To remove deleted records, disconnect, wait for device.disconnected, and connect again.
Sync Now
const result = await Sonar.sync();
// result.status is "started" or "already_running"
Call sync() when the user expects fresh data, for example when they open a health screen after a workout. It sends new data, then continues any unfinished history. It does not read everything again. A call during an active sync returns already_running, and the SDK syncs once more after it.
Resync
await Sonar.resync("apple_health");resync reads everything again: the last 30 days, then the rest of the history period. It also picks up a new historyDays. The connection and the data in Sonar stay. Sonar skips the records it already holds.
Use it when data looks incomplete, after the user allows more types, or to pick up edits to older data. It is not a routine refresh, and it never deletes data.
When Something Goes Wrong
The SDK handles most problems itself. Your app only needs to follow recoveryAction in the state.
| Situation | What happens | What your app does |
|---|---|---|
| No network, or Sonar briefly unavailable | The SDK sends the same data on its next sync. Nothing is lost | Nothing |
| The SDK session ended | The SDK renews it through your token provider | Call authenticate if the state asks for it |
| The connection moved to another phone | This phone stops syncing and shows disconnected |
Offer to connect again |
| The user never answered the permission sheet for some Health types | The other types sync. The state shows request_permission |
Call connect again to show the sheet |
| Health is restricted on the phone | Syncing stops. The state shows open_settings |
Ask the user to allow Health access in Settings |
| Sonar refused data | The state shows contact_support |
Send diagnostics and contact support |
Removing the app from the phone loses the SDK’s progress. After a reinstall, the SDK picks the user’s connection up when they sign in (see Picking the Connection Up) and imports the history period again. Sonar skips what it already holds.
When Data Shows Up in the API
Data the SDK has sent is not readable until Sonar processes it. Sonar processes new data shortly after it arrives, and each processing run that changes data sends one subject.synced webhook. After a first connect, expect one webhook for the recent days and more as the history arrives. Your backend can read daily metrics, sleep, workouts, scores, and timeseries as soon as the first one arrives.
There is no webhook for “history import finished”. On the phone, backfill.phase shows complete.
Timezone
The SDK reports the phone’s timezone to Sonar. Sonar uses it as the user’s timezone unless your backend set profile.timezone. When it changes, for example when the user travels, Sonar recomputes the user’s days in the new timezone. See User Timezone.
Sonar