backgroundTasks
La traduction de cette page manque ou n'est plus à jour : elle s'affiche en anglais. Lire en anglais
Ask the operating system to run occasional work while your app is not in front. Use it for opportunistic sync or maintenance, not an exact timer.
Declare the task in app configuration
// kuito.config.ts
import type { KuitoConfig } from 'kuito/config';
export default {
name: 'Reel',
ios: { bundleIdentifier: 'com.example.reel' },
android: { applicationId: 'com.example.reel' },
backgroundTasks: ['sync'],
} satisfies KuitoConfig;Define and schedule a sync task
import { Button, Text, VStack, backgroundTasks, storage, task } from 'kuito';
// At module scope: a background launch runs the app's modules, not its UI.
backgroundTasks.define('sync', async ({ deadline }) => {
const remaining = deadline - Date.now() - 1000;
if (remaining <= 0) return;
const response = await fetch('https://example.com/feed', {
signal: AbortSignal.timeout(remaining),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
await storage.set('feed', await response.text());
});
export default function App() {
const enable = task(async () => {
if ((await backgroundTasks.status()) !== 'available')
throw new Error('Background refresh is unavailable');
await backgroundTasks.schedule('sync', { minimumInterval: 60, requiresNetwork: true });
});
return (
<VStack>
<Button disabled={enable.running.value} onPress={() => void enable.run().catch(() => {})}>
Enable background sync
</Button>
<Text>{enable.error.value ? String(enable.error.value) : ''}</Text>
</VStack>
);
}Behavior and limits
Define a named task at module scope, declare that name in config, then schedule it. The operating system chooses when it runs and may delay or skip execution. Keep work bounded, finish before the supplied deadline, and make repeated runs safe. status reports whether OS policy allows background tasks; cancel removes a schedule. Background work has no mounted screen to update.
Continue
Read App state for the related workflow. The API reference lists every method, option, event and return type.
Référence
Exports 8
Méthodes, constantes et types exposés par cet espace de noms natif.
function
defineThe task the OS runs under name.functionscheduleAsks the OS to run the task about every minimumInterval minutes, across restarts.typeBackgroundTaskStatusWhether the OS lets this app run in the background: the user or a policy can turn it off.typeBackgroundTaskOptionsTells the OS how often a background task may run and what the device must have (a network, a charger) before it does.typeBackgroundTaskRunWhat a task receives when the OS runs it.typeBackgroundTaskResolve when the work is done; throw or reject to report a failed run.functioncancelStops the OS from running the task.functionstatusWhether the OS lets this app run in the background.Généré depuis les déclarations TypeScript publiques : signatures, props et valeurs par défaut viennent de la source vérifiée, les descriptions de sa JSDoc. Aucune valeur par défaut n'est inventée.