How would you make an Android upload resilient with WorkManager?

Instruction: Design a deferrable file upload that can resume after ordinary process exit or reboot. Explain durable input, unique work, constraints, retry versus terminal failure, cancellation and duplicate server-side effects.

Context: Apply WorkManager to one upload lifecycle. Distinguish scheduling persistence from exact timing or exactly-once remote execution; no code implementation is required.

Updated

Official answer

I would persist the upload's local record and stable operation ID, then enqueue an appropriate one-time WorkRequest for a CoroutineWorker. Pass a small record ID, not the file contents, and ensure the worker can reopen the file after process recreation. Use unique work with a deliberate existing-work policy to avoid redundant local schedules.

The worker validates its input, reads current state and uploads using a server-supported idempotency key. Return success after the intended result is durably recognized. Retry transient network/server failures with backoff; report invalid input or an unrecoverable authorization problem as a terminal/actionable outcome rather than retry forever. Respect cancellation. A timeout after server acceptance may require checking the operation ID because the remote write might already have happened.

Add network or charging constraints that match the requirement. UNMETERED describes network cost classification, not specifically Wi-Fi. Observe WorkInfo through the UI's lifecycle and expose pending, running, failed and completed states. Unique scheduling alone does not make a remote side effect exactly once.

WorkManager can persist scheduling across ordinary process exit and reboot, but execution still depends on constraints, quotas and platform/user restrictions; cancellation, force-stop and uninstall matter. It is not an exact-time scheduler. Measure queue delay separately from execution time and end-to-end completion. Test a duplicate enqueue, replay after an uncertain upload result, missing local file and denied connectivity.

Work requests, Manage unique work

Related Questions