Keep Data Fresh with Expo Background Fetch
Learn how to run periodic background tasks in Expo to sync data even when the app is closed, using expo-task-manager and expo-background-fetch.
Insight
Expo's BackgroundFetch API lets you schedule lightweight tasks that run while your app is in the background or terminated. It's perfect for pulling remote data, refreshing auth tokens, or pre‑caching content. Unlike push notifications, the OS decides the exact timing, so keep the work short (< 30 s) and idempotent.
Example
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
const FETCH_TASK = 'background-sync';
TaskManager.defineTask(FETCH_TASK, async () => {
// Fetch new data silently
const res = await fetch('https://api.example.com/updates');
const json = await res.json();
// Store in async storage or MMKV
await AsyncStorage.setItem('latest', JSON.stringify(json));
return BackgroundFetch.Result.NewData;
});
await BackgroundFetch.registerTaskAsync(FETCH_TASK, {
minimumInterval: 15 * 60, // 15 minutes
stopOnTerminate: false,
startOnBoot: true,
});
Takeaway
Register a short, pure function with defineTask and let the OS handle scheduling. Keep network calls minimal and always return a BackgroundFetch.Result to inform the system of success or failure. This pattern ensures your app stays up‑to‑date without draining the battery.