Fixed
Status Update
Comments
ap...@google.com <ap...@google.com> #2
So you can do this now by doing something like:
@Suppress("UNCHECKED_CAST")
class DelegateWorker: Worker() {
companion object {
const val DELEGATE_WORKER_NAME = "DELEGATE_WORKER_NAME"
const val PERIOD = "PERIOD" // in minutes.
}
override fun doWork(): Result {
val delegateName = inputData.getString(DELEGATE_WORKER_NAME) ?: return Result.FAILURE
val delegateKlass = Class.forName(delegateName) as? Class<out Worker> ?: return Result.FAILURE
val period = inputData.getLong(PERIOD, 15)
// build your worker with the constraints you want.
val request = PeriodicWorkRequest.Builder(delegateKlass, period, TimeUnit.MINUTES)
.setInputData(inputData)
.build()
WorkManager.getInstance()
.enqueue(request)
return Result.SUCCESS
}
}
class PeriodicWorker: Worker() {
override fun doWork(): Result {
// Do some periodic work
return Result.SUCCESS
}
}
// You want to delay the PeriodicWorker. You can use initial delay on the DelegateWorker, which in
// turn enqueue's PeriodicWorker.
val data = mapOf(DelegateWorker.DELEGATE_WORKER_NAME to PeriodicWorker::class.java.name ).toWorkData()
val delayedPeriodicWorkRequest = OneTimeWorkRequestBuilder<DelegateWorker>()
.setInitialDelay(10, TimeUnit.SECONDS)
.build()
WorkManager.getInstance()
.enqueue(delayedPeriodicWorkRequest)
The idea is to create a OneTimeWorkRequest with an initial delay, which in turn calls an enqueue() on the PeriodicWorkReqeust.
@Suppress("UNCHECKED_CAST")
class DelegateWorker: Worker() {
companion object {
const val DELEGATE_WORKER_NAME = "DELEGATE_WORKER_NAME"
const val PERIOD = "PERIOD" // in minutes.
}
override fun doWork(): Result {
val delegateName = inputData.getString(DELEGATE_WORKER_NAME) ?: return Result.FAILURE
val delegateKlass = Class.forName(delegateName) as? Class<out Worker> ?: return Result.FAILURE
val period = inputData.getLong(PERIOD, 15)
// build your worker with the constraints you want.
val request = PeriodicWorkRequest.Builder(delegateKlass, period, TimeUnit.MINUTES)
.setInputData(inputData)
.build()
WorkManager.getInstance()
.enqueue(request)
return Result.SUCCESS
}
}
class PeriodicWorker: Worker() {
override fun doWork(): Result {
// Do some periodic work
return Result.SUCCESS
}
}
// You want to delay the PeriodicWorker. You can use initial delay on the DelegateWorker, which in
// turn enqueue's PeriodicWorker.
val data = mapOf(DelegateWorker.DELEGATE_WORKER_NAME to PeriodicWorker::
val delayedPeriodicWorkRequest = OneTimeWorkRequestBuilder<DelegateWorker>()
.setInitialDelay(10, TimeUnit.SECONDS)
.build()
WorkManager.getInstance()
.enqueue(delayedPeriodicWorkRequest)
The idea is to create a OneTimeWorkRequest with an initial delay, which in turn calls an enqueue() on the PeriodicWorkReqeust.
hu...@google.com <hu...@google.com>
ap...@google.com <ap...@google.com> #3
This looks like a hack, instead of just adding setInitialDelay method on the periodic worker that will just run once.
ap...@google.com <ap...@google.com> #4
I have tried going the route of the OneTimeWorkRequest with an initial delay that triggers a PeriodicWorkRequest but it doesn't seem to work. It would be much nicer to be able to add a simple delay to the first occurence.
ap...@google.com <ap...@google.com> #5
#3: This is exactly what an internal implementation would look like because the backing JobScheduler does not support initial delays on periodic work. I realize that WorkManager doing it for you is nicer, which is why this feature request is still open, but it may take us some time to get to it.
#4: Please file a separate bug if that doesn't work for you.
#4: Please file a separate bug if that doesn't work for you.
ap...@google.com <ap...@google.com> #6
#5: How could I debug the behaviour of this chain of Work?
an...@google.com <an...@google.com> #7
Use logcat, or use the various getStatus methods to see what's going on. File a bug with a bugreport and sample code that results in the problem.
ap...@google.com <ap...@google.com> #8
It would be nice to see setInitialDelay() on PeriodicWorkRequest.Builder in the next version, Thanks
Description
When the user specifies a destination to store the result of an image capture (with the use of
outputFileOptions
), theImageCapture
use case should verify whether this destination is valid before triggering the image capture request to the camera. If the destination is not valid, the operation should fail fast and the user should be notified about it.