Background Fetch Made Simple with Expo Task Manager
Learn how to schedule periodic background work in Expo apps using Task Manager, keeping data fresh even when the app is closed.
Insight
Expo's TaskManager lets you run code in the background without ejecting or writing native modules. By defining a task and registering it with BackgroundFetch, you can pull remote data, update local storage, or schedule notifications while the app is idle. The API is lightweight, works on both iOS and Android, and respects platform limits on background execution.
Example
import * as TaskManager from 'expo-task-manager';
import * as BackgroundFetch from 'expo-background-fetch';
import AsyncStorage from '@react-native-async-storage/async-storage';
const FETCH_TASK = 'fetch-latest-news';
TaskManager.defineTask(FETCH_TASK, async () => {
const response = await fetch('https://example.com/api/news');
const data = await response.json();
await AsyncStorage.setItem('latestNews', JSON.stringify(data));
return BackgroundFetch.Result.NewData;
});
await BackgroundFetch.registerTaskAsync(FETCH_TASK, {
minimumInterval: 15 * 60, // 15 minutes
stopOnTerminate: false,
startOnBoot: true,
});
Takeaway
Register a single background task for any periodic sync need, and let Expo handle the native plumbing. Keep the task short, avoid heavy UI work, and always return a BackgroundFetch.Result to let the OS manage scheduling efficiently.