Change theme
Help
Press space for more information.
Show links for this issue (Shortcut: i, l)
Copy issue ID
Previous Issue (Shortcut: k)
Next Issue (Shortcut: j)
Sign in to use full features.
Vote: I am impacted
Notification menu
Refresh (Shortcut: Shift+r)
Go home (Shortcut: u)
Pending code changes (auto-populated)
View issue level access limits(Press Alt + Right arrow for more information)
Unintended behavior
View staffing
Description
Devices/Android versions reproduced on: Android Emulator api28
I have SyncWorker which I wrap in OneTimeWorkRequest and enqueue by WorkManager but the problem is that this worker is executed only once, no matter how many times I enqueue it.
I create OneTimeWorkRequest.Builder() with common configuration
private val syncWorkBuilder = OneTimeWorkRequest.Builder(SyncWorker::class.java)
.setConstraints(Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build())
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
Then I reuse it by changing some configuration that might wary between invocations and enqueue
fun sync(triggerImmediate: Boolean = false, syncFromScratch: Boolean = false, delay: Int = 0) {
val workManager = WorkManager.getInstance()
if ( workManager == null
|| workManager.getStatusesByTag(SyncWorker.SYNC_FULL_TAG).value
?.find { it.state == State.RUNNING || it.state == State.ENQUEUED } != null) {
return
}
val startDelaySeconds = if (triggerImmediate) 0L else 2L + delay
val syncWork = syncWorkBuilder
.addTag(if (syncFromScratch) SyncWorker.SYNC_INITIAL_TAG else SyncWorker.SYNC_FULL_TAG)
.setInitialDelay(startDelaySeconds, TimeUnit.SECONDS)
.build()
Timber.i("Sync work enqueued $syncWork")
workManager.enqueue(syncWork)
}
It appeared that if I'm not reusing Builder but creating new every time it works fine
I think the problem is in constructor `mId` is crated there and is the same for this instance of builder
public Builder(@NonNull Class<? extends Worker> workerClass) {
mId = UUID.randomUUID();
mWorkSpec = new WorkSpec(mId.toString(), workerClass.getName());
addTag(workerClass.getName());
}