Skip to main content

Usage: Sync health data manually

Sync Apple Health 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
SummaryUTCN/AYesterdayAHRookSummaryManager
EventUTCN/ATodayAHRookEventManager

Sync summaries

To sync any type of summary, you need to provide a date. This date cannot be the current day. 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-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 AHRookSummaryManager.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 Apple Health
  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() {
AHRookSummaryManager.syncPendingSummaries().then((_) {
// Success
}).catchError((exception) {
// Handle error
});
}

Recommendations

Summaries are collections of health data from a past day, so it's not necessary to sync them many times per day.

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

try {
await AHRookSummaryManager.syncSleepSummary(yesterday);

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

// Sync for other types of summaries...

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

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

Sync events

To sync any type of event, you need to provide a date. 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-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 AHRookEventManager.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 Apple Health
  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() {
AHRookEventManager.syncPendingEvents().then((_) {
// Success
}).catchError((exception) {
// Handle error
});
}

Recommendations

Events are collections of health data divided in intervals of 1 hour, so you can/should sync them frequently. Our recommendation is that every time your app is opened you should sync events.

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

try {
await AHRookEventManager.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 AHRookEventManager.syncPendingEvents();

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