Skip to content

Instantly share code, notes, and snippets.

@billmalarky
Last active January 5, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billmalarky/cfc85546149bacf4c11d5ad263cbd40b to your computer and use it in GitHub Desktop.
Save billmalarky/cfc85546149bacf4c11d5ad263cbd40b to your computer and use it in GitHub Desktop.
background task snippet
import BackgroundTask from 'react-native-background-task'
import queueFactory from 'react-native-queue';
BackgroundTask.define(async () => {
// Init queue
queue = await queueFactory();
// Register job worker
queue.addWorker('pre-fetch-image', async (id, payload) => {
Image.prefetch(payload.imageUrl);
});
// Start the queue with a lifespan
// IMPORTANT: OS background tasks are limited to 30 seconds or less.
// NOTE: Queue lifespan logic will attempt to stop queue processing 500ms less than passed lifespan for a healthy shutdown buffer.
// IMPORTANT: Queue processing started with a lifespan will ONLY process jobs that have a defined timeout set.
// Additionally, lifespan processing will only process next job if job.timeout < (remainingLifespan - 500).
await queue.start(25000); // Run queue for at most 25 seconds.
// finish() must be called before OS hits timeout.
BackgroundTask.finish();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment