Skip to content

Instantly share code, notes, and snippets.

@kasem-sm
Last active May 31, 2022 01:59
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 kasem-sm/22eb3ca2095c7bfb2b6ecc4ceae997b9 to your computer and use it in GitHub Desktop.
Save kasem-sm/22eb3ca2095c7bfb2b6ecc4ceae997b9 to your computer and use it in GitHub Desktop.
When Jetpack's Glance met his fellow worker, Work Manager
// Call on Application Start
fun execute() = enqueueWorker()
private fun enqueueWorker() {
workManager.enqueueUniquePeriodicWork(
"daily_read_worker_tag",
// KEEP documentation:
// If there is existing pending (uncompleted) work with the same unique name, do nothing.
// Otherwise, insert the newly-specified work.
ExistingPeriodicWorkPolicy.KEEP,
buildRequest()
)
}
private fun buildRequest(): PeriodicWorkRequest {
// 1 day
return PeriodicWorkRequestBuilder<DailyReadWorkerTask>(24, TimeUnit.HOURS)
.addTag("daily_read_worker_tag")
.setConstraints(
Constraints.Builder()
// Network required
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment