~/swaraj.dev
Back to all posts
April 28, 20261 min read

Background Fetch in Expo: Keep Data Fresh Without UI

Learn how to use Expo's BackgroundFetch and TaskManager to run periodic data syncs even when the app is closed, improving offline reliability.

expobackgroundfetchreact-native

Insight

Expo provides a lightweight way to schedule background tasks that run even when the app is not in the foreground. By combining expo-background-fetch with expo-task-manager, you can pull fresh data (e.g., notifications, sync queues) at regular intervals without keeping the UI alive. This pattern is ideal for offline‑first apps that need to stay up‑to‑date without user interaction.

Example

// backgroundTask.ts
import * as TaskManager from 'expo-task-manager';
import * as BackgroundFetch from 'expo-background-fetch';
import { fetchUpdates } from './api';

const TASK_NAME = 'background-sync';

TaskManager.defineTask(TASK_NAME, async () => {
  try {
    await fetchUpdates(); // your async data sync
    return BackgroundFetch.Result.NewData;
  } catch {
    return BackgroundFetch.Result.Failed;
  }
});

export async function registerBackgroundSync() {
  await BackgroundFetch.registerTaskAsync(TASK_NAME, {
    minimumInterval: 15 * 60, // 15 minutes
    stopOnTerminate: false,
    startOnBoot: true,
  });
}

Takeaway

Register the task once (e.g., in App.tsx with useEffect) and let the OS handle execution. Keep the task short and idempotent, and always return the appropriate BackgroundFetch.Result to avoid unnecessary battery drain.