Does one time work request interfere with periodic work request?

I have a periodic work request in my app which runs a ListenableWorker and I also have a one-time work request which runs the same Listenable worker. Would this affect the periodic work request? Say if 2 minutes are remaining for the next periodic work request but I run the one-time work request for the same work, would the periodic work request continue after the completion of the one-time work request?

1 answer

  • answered 2020-06-27 09:44 stewemetal

    OneTimeWorkRequests and PeriodicWorkRequests don't interfere with each other as far as WorkManager is concerned. You can schedule one-time works and periodic works using the same Worker class. The Worker class you provide is basically the bag of operations you hand over to WorkManager to be deferred, as in executed as soon as every given constraint is met. That being said, if you have a ListenableWorker subclass named SyncWorker and you schedule it to run once every hour periodically like this:

    val periodicWorkRequest = PeriodicWorkRequestBuilder<SyncWorker>(1, TimeUnit.HOURS).build()
    WorkManager.getInstance(...).enqueuePeriodicWork(periodicWorkRequest)
    

    and you schedule a one-time work with the same class to run as soon as possible:

    val oneTimeWork = OneTimeWorkRequest.from(SyncWorker::class.java)
    WorkManager.getInstance(...).enqueue(oneTimeWork)
    

    from WorkManager's point of view, these count as different, unrelated work instances. Now to answer your question, WorkManager will just do its thing. It will run both work instances even if you start SyncWorker as a OneTimeWorkRequest 2 minutes before the scheduled runinng of the periodic work.

    The only thing you have to consider is if running SyncWorker (or your equivalent of it) multiple times frequently could cause your data or business logic any harm.