-
-
Save kasem-sm/22eb3ca2095c7bfb2b6ecc4ceae997b9 to your computer and use it in GitHub Desktop.
When Jetpack's Glance met his fellow worker, Work Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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