Skip to main content

Usage: Sync health data manually

Sync Health Connect data by yourself.

info

The automatic implementation is faster and easier to integrate and is what most developers need, try to use it before attempting to create a custom sync process.

Sync health data manually

There are 2 types of health data Summaries and Events.

Health DataTimezoneOldest date of retrievalLatest date of retrievalClass
SummaryUTC29 daysYesterdayHCRookSummaryManager
EventUTC29 daysTodayHCRookEventManager

Sync summaries

To sync any type of summary, you need to provide a date. This date cannot be the current day and cannot be older than 29 days. See the examples below:

Current dateProvided dateIs valid?
2023-01-082023-01-08No, the date is from today
2023-01-082023-01-07Yes, the date is from yesterday
2023-01-082022-11-01No, the date is older than 29 days
2023-01-082023-01-01Yes, the date is 7 days old

To get health data, call sync_xxx_summary and provide a LocalDate instance of the day you want to retrieve the data from.

For example, if you want to sync yesterday's sleep summary, call syncSleepSummary.

Future<void> syncYesterdaySleepSummary() async {
final today = DateTime.now();
final yesterday = today.subtract(const Duration(days: 1));

try {
await HCRookSummaryManager.syncSleepSummary(yesterday);

// Success
} catch (exception) {
// Handle error
}
}

Sync pending summaries

When you call sync_xxx_summary, what happens it's that:

  1. Health data is extracted from Health Connect
  2. The extracted data is enqueued
  3. The enqueued data is uploaded to ROOK servers.
  4. If success the queued is cleared. Otherwise, the summary is stored.

To retry sending those stored summaries call syncPendingSummaries

void syncPendingSummaries() {
HCRookSummaryManager.syncPendingSummaries().then((_) {
// Success
}).catchError((exception) {
// Handle error
});
}

Recommendations

Summaries are collections of health data from a past day, so you should not sync them more than once a day.

/// This function assumes that you have already checked availability and permissions.
Future<void> syncSummaries() async {
final yesterday = today.subtract(const Duration(days: 1));

try {
await HCRookSummaryManager.syncSleepSummary(yesterday);

// Success
} catch (exception) {
// Handle error
}

// Sync other types of summaries...

// Finally you can call syncPendingSummaries to retry all failed uploads (optional)
// try {
// await HCRookSummaryManager.syncPendingSummaries();
//
// // Success
// } catch (exception) {
// // Handle error
// }
}
  • Be cautions of syncing summaries an excessive amount of times, Health Connect has a daily usage limit and your app could be blocked for some hours or a full day.

Sync events

To sync any type of event, you need to provide a date. This date cannot be older than 29 days. See the examples below:

Current dateProvided dateIs valid?
2023-01-082023-01-08Yes, the date is from today
2023-01-082023-01-07Yes, the date is from yesterday
2023-01-082022-11-01No, the date is older than 29 days
2023-01-082023-01-01Yes, the date is 7 days old

To get health data, call sync_xxx_events and provide a LocalDate instance of the day you want to retrieve the data from.

For example, if you want to sync today's physical events, call syncPhysicalEvents.

Future<void> syncTodayPhysicalEvents() async {
final today = DateTime.now();

try {
await HCRookEventManager.syncPhysicalEvents(today);

// Success
} catch (exception) {
// Handle error
}
}

Sync pending events

When you call sync_xxx_events, what happens it's that:

  1. Health data is extracted from Health Connect
  2. The extracted data is enqueued
  3. The enqueued data is uploaded to ROOK servers.
  4. If success the queued is cleared. Otherwise, the events is stored.

To retry sending those stored events call syncPendingEvents

void syncPendingEvents() {
HCRookEventManager.syncPendingEvents().then((_) {
// Success
}).catchError((exception) {
// Handle error
});
}

Recommendations

Events are collections of health data divided into intervals of one hour, so you can and should sync them frequently. We recommend that every time your app is opened, you sync events.

Future<void> syncTodayPhysicalEvents() async {
final today = DateTime.now();

try {
await HCRookEventManager.syncPhysicalEvents(today);

// Success
} catch (exception) {
// Handle error
}

// Sync other types of events...

// Finally you can call syncPendingEvents to retry all failed uploads (optional)
try {
await HCRookEventManager.syncPendingEvents();

// Success
} catch (exception) {
// Handle error
}
}
  • Be cautious not to sync events excessively. Health Connect has a daily usage limit, and your app could be blocked for some hours or a full day.