Usage: Sync health data manually
Sync Apple Health data by yourself.
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 Data | Timezone | Oldest date of retrieval | Latest date of retrieval | Class |
---|---|---|---|---|
Summary | UTC | N/A | Yesterday | AHRookSummaryManager |
Event | UTC | N/A | Today | AHRookEventManager |
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 date | Provided date | Is valid? |
---|---|---|
2023-01-08 | 2023-01-08 | No, the date is from today |
2023-01-08 | 2023-01-07 | Yes, the date is from yesterday |
2023-01-08 | 2023-01-01 | Yes, 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:
- Health data is extracted from Apple Health
- The extracted data is enqueued
- The enqueued data is uploaded to ROOK servers.
- 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 date | Provided date | Is valid? |
---|---|---|
2023-01-08 | 2023-01-08 | Yes, the date is from today |
2023-01-08 | 2023-01-07 | Yes, the date is from yesterday |
2023-01-08 | 2023-01-01 | Yes, 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:
- Health data is extracted from Apple Health
- The extracted data is enqueued
- The enqueued data is uploaded to ROOK servers.
- 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
}
}