Skip to content

Instantly share code, notes, and snippets.

@akueisara
Last active June 26, 2022 23:20
Show Gist options
  • Save akueisara/490fe1df2b555cf7b926356810232553 to your computer and use it in GitHub Desktop.
Save akueisara/490fe1df2b555cf7b926356810232553 to your computer and use it in GitHub Desktop.
val request = OneTimeWorkRequestBuilder<YourWorker>()
// OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST, which causes the job to run as an ordinary work request
// OutOfQuotaPolicy.DROP_WORK_REQUEST, which causes the request to cancel if there is not sufficient quota.
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
WorkManager.getInstance(context).enqueue(request)
// For example, sync operations: you want to sync
// when you first launch the app
// every 12 to 24 hours to get the refreshest data
// when your language changes
// => you only want one sync active at a time
val syncRequest: OneTimeWorkRequest = ...
workManager
.beginUniqueWork(
uniqueWorkName = "sync",
// What to do if there's a work name with the "sync" in-flight already
// There are three types of work policies:
// KEEP: existing work and ignore the new work.
// REPLACE: existing work with the new work.
// APPEND: the new work to the end of the existing work.
// The existing work becomes a `prerequisite` to the new work.
// APPEND_OR_REPLACE: similar to APPEND, except that it is not dependent on `prerequisite` work status.
// If the existing work is CANCELLED or FAILED, the new work will replace it.
existingWorkPolicy = KEEP,
work = syncRequest)
.enqueue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment