I am enqueuing future work while also running the initial job "now". After 20 items get enqueued any new items that get added no longer run immediately and I'm going to have to wait 30 days to see if they run at all ;)
I have waited more than 24 hours now after setting the initial delay for a work item as 10 seconds (the minimum allowed).
If I schedule a 21st job, nothing happens. Cancel any of the initial 20 and the 21st job runs after 10 seconds. 100% repeatable -- reschedule job 20, nothing. Cancel job 21 and job 20 will run after 10 seconds.
dependencies {
implementation 'androidx.work:work-runtime:2.3.4'
}
fun scheduleTickle(context: Context, item: Item) {
// item.id is the auto-generated primary key from the database
val periodicWorkRequest = PeriodicWorkRequest.Builder(
MessageWorker::class.java,
item.minimumInterval,
TimeUnit.DAYS
).let {
it.setInputData(
Data.Builder()
.putLong("EXTRA_KEY_ITEM_ID", item.id)
.build()
)
it.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
it.addTag("com.example.myapp.periodicWork.${item.id}")
it.setInitialDelay(10, TimeUnit.SECONDS)
}.build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
item.id.toString(),
ExistingPeriodicWorkPolicy.REPLACE,
periodicWorkRequest
)
}