Running Periodic Background Sync with Expo Task Manager
Learn how to schedule reliable background fetches in Expo apps using Task Manager, turning periodic data sync into a native‑level feature.
Insight
Mobile apps often need to stay fresh without user interaction, but many developers assume React Native can’t run reliable background code. Expo’s expo-task-manager (paired with expo-background-fetch) lets you register native‑level tasks that fire even when the app is closed, giving you a true offline‑first sync loop. On iOS the system decides the exact cadence, while Android offers more deterministic intervals via headless JS.
Example
import * as TaskManager from 'expo-task-manager';
import * as BackgroundFetch from 'expo-background-fetch';
const SYNC_TASK = 'background-sync';
TaskManager.defineTask(SYNC_TASK, async () => {
// Fetch fresh data and store it locally
await fetchAndPersist();
return BackgroundFetch.Result.NewData;
});
await BackgroundFetch.registerTaskAsync(SYNC_TASK, {
minimumInterval: 15 * 60, // 15 minutes
stopOnTerminate: false,
startOnBoot: true,
});
Takeaway
Register a single background task and let the OS handle scheduling; keep the task lightweight and idempotent. Remember to request the appropriate permissions and test on real devices, as simulators don’t trigger background fetches.