android-workmanagerlisted
Install: claude install-skill lenorebreakneck630/claude-zero-to-hero-android-KMP
# Android WorkManager
## When to Use It
Use WorkManager for work that is:
- deferrable
- guaranteed eventually
- expected to continue across app restarts or process death
- constrained by network, charging, battery, or storage conditions
Do **not** use WorkManager for:
- immediate in-app UI actions
- work that must finish while the screen is visible only
- exact alarms
- long-running foreground media playback
---
## Core Principles
- Workers must be **idempotent** — safe to run more than once.
- Prefer **unique work** so duplicate jobs do not pile up.
- Keep business logic in domain/data classes; the worker only coordinates execution.
- Return `Result.retry()` only for failures that may succeed later.
- Pass IDs and lightweight flags through `Data`, not large payloads.
---
## Worker Structure
Use `CoroutineWorker` for suspending APIs:
```kotlin
class SyncNotesWorker(
appContext: Context,
params: WorkerParameters,
private val noteRepository: NoteRepository
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
return when (noteRepository.sync()) {
is com.example.core.domain.Result.Success -> Result.success()
is com.example.core.domain.Result.Error -> Result.retry()
}
}
}
```
Map typed domain/data failures to WorkManager results intentionally:
- temporary network failure -> `Result.retry()`
- invalid input or permanent server rejection -> `Result.failure()`
- success -> `Result.su