The library allows you to reconnect to downloads or uploads that continued running while your app was closed or terminated by the OS.
To sync your UI with ongoing background transfers upon app restart, use getExistingDownloadTasks() or getExistingUploadTasks(). This returns an array of task objects that you can then attach listeners (.progress(), .done(), .error()) to.
Tip: Use meaningful, persistent task IDs instead of random UUIDs so you can easily match tasks to your UI components after a restart.
import { getExistingDownloadTasks, getExistingUploadTasks } from '@kesha-antonov/react-native-background-downloader'
// Re-attaching to downloads
const lostTasks = await getExistingDownloadTasks()
for (const task of lostTasks) {
task.progress(({ bytesDownloaded, bytesTotal }) => {
console.log(`Downloaded: ${bytesDownloaded / bytesTotal * 100}%`)
}).done(({ location, bytesDownloaded, bytesTotal }) => {
console.log('Download complete!', { location, bytesDownloaded, bytesTotal })
})
}
// Re-attaching to uploads
const lostUploads = await getExistingUploadTasks()
for (const task of lostUploads) {
task.progress(({ bytesUploaded, bytesTotal }) => {
console.log(`Uploaded: ${bytesUploaded / bytesTotal * 100}%`)
}).done(({ responseCode, responseBody }) => {
console.log('Upload complete!', { responseCode, responseBody })
})
}