Skip to content

Instantly share code, notes, and snippets.

@kasem-sm
Last active May 31, 2022
Embed
What would you like to do?
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