Working with WorkManager (Android Dev Summit '18)

Android Developers · 43K views · 2018 · 29:28

Open on YouTube

Software (other)Software practice, open source & careerstalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering WorkManager questions about async workers, cancelling and stopping work, chaining, periodic requests and custom executors, since the talk is by the library's own engineers.

Recommended by 2 people across 5 different months, 2018 to 2019.

What it is

A 29-minute Android Dev Summit 2018 session by Google engineers Rahul Ravikumar and Sumir Kataria on WorkManager and its API. It covers common use cases, new post-alpha APIs such as non-blocking ListenableWorkers, and how WorkManager works alongside coroutines and RxJava.

What people said

Strong rec pfmaggi · 2018-11 · Stack Overflow · on “Periodic Work Request not executing doWork in Work Manager”

Given the nature of the work you want to do (download something from the network) I would probably use a ListenableWorker and a ResolvableFuture that have been introduced in one of the last alpha of WorkManager. You can find some example in this video around minute 11 [this video] . BTW, what the full video, if you plan to use WorkManager it's a treasure chest of information.

Recommends pfmaggi · 2018-11 · Stack Overflow · on “How to know if a Worker has failed being triggered/executed by the queue?”

Using ExistingWorkPolicy.APPEND , you´re creating a chain of work. If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes. One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById() . You can take a look at this talk [this video] presented at the Android Developer Summit that covers this and other example.

Recommends pfmaggi · 2018-11 · Stack Overflow · on “WorkManager. How to cancel appended work?”

You should use the isStopped() [LINK] method call inside your onWork method This is explained in the video presented at the Android Developer Summit. Around minute 15 into the presentation there's a whole section on how to stop work in WorkManager that goes through these details [this video]

Recommends pfmaggi · 2018-12 · Stack Overflow · on “How to make sure that WorkManager cancels my Worker?”

As you wrote WorkManager can only try with a best-effort to cancel a work. In particular, if a task is scheduled to run, and you cancel it, WorkManager will remove it from the schedule. However, if the tasks it's already running, WorkManager cannot safely interrupt it. The best option is that you write your Worker class taking care of an external cancellation. This has been covered in the Working with WorkManager talk (around minute 15) [this video] recorded at the Android Developer Summit 2018. To be a good citizen you can pool WorkManager using the method: ListenableWorker.isStopped() [LINK] . You can combine this with the onStopped [LINK] callback to cleanup your code when you or the OS reques

Recommends pfmaggi · 2019-01 · Stack Overflow · on “WorkManager backoff delay doesn't work on some devices”

Edit: WorkManager 1.0.0-beta02 [LINK] has been released with a fix for a bug that seems related to the problem you've [LINK] Fixed an edge case where periodic work could run more than once per interval on devices running Android 6.0 (API level 23). Original Answer: You should try to avoid to enqueue the WorkRequest every time that the application starts. WorkManager takes care of resuming the job for you and you don't need to do it. For more information on WorkManager, and what it can do for you, you should take a look at this blog series [LINK] . There's also the video from Android Developer Summit talk "Working with WorkManager" [this video] that explain this. If you need to enqueue new works e

Recommends pfmaggi · 2019-09 · Stack Overflow · on “Co Routines get location from background”

The Worker [LINK] class only supports synchronous work. This means that when you return the result, the code in your worker needs to have completed its execution. In your case you should use a ListenerWorker [LINK] (or a CoroutineWorker [LINK] if you prefer to use Kotlin). Take a look at last year ADS talk on WorkManager [this video] , it covers this a bit. You can also view my talk Embracing WorkManager [LINK] that covers the difference between the different Worker classes.

Recommends pfmaggi · 2019-09 · Stack Overflow · on “Android GPS requestLocationUpdates weird behavior”

If you're using a Worker (you just pasted a line of the worker, so I'm just guessing), it only supports synchronous code. Take a look at last year ADS talk on WorkManager, it covers this a bit: [this video]

Recommends Pulkit · 2019-11 · Stack Overflow · on “How to handle app kill scenario in WorkManager?(workaround)”

You don't do anything manually. Workmanager internally works, and restarts itself, even when you app is killed by the user. (Which is, Swiped away from recent screen, or killed by CPU while app in background). I would advice watching this video [this video] to understand more

Cites art · 2018-11 · Stack Overflow · on “Dagger2: Unable to inject dependencies in WorkManager”

I use Dagger2 Multibindings [LINK] to solve this problem. The similar approach is used to inject ViewModel objects (it's described well here [LINK] ). Important difference from view model case is the presence of Context and WorkerParameters arguments in Worker constructor. To provide these arguments to worker constructor intermediate dagger component should be used. Annotate your Worker 's constructor with @Inject and provide your desired dependency as constructor argument. class HardWorker @Inject constructor(context: Context, workerParams: WorkerParameters, private val someDependency: SomeDependency) : Worker(context, workerParams) { override fun doWork(): Result { // do some work with use

Cites Oliver · 2019-01 · Stack Overflow · on “How do I run a ListenableWorker work on a background thread?”

Since I need to perform work asynchronously in WorkManager, I need to use the ListenableWorker , which by default runs on the main (UI) thread. Since this work could be a long processing tasks that could freeze the interface, I wanted to perform it on a background thread. In the Working with WorkManager (Android Dev Summit '18) video [this video] , the Google engineer showed how to manually configure WorkManager to run works on a custom Executor , so I followed his guidance: 1) Disable the default WorkManager initializer in the AndroidManifest: <provider android:name="androidx.work.impl.WorkManagerInitializer" android:authorities="com.example.myapp.workmanager-init" tools:node="remove" /> 2) In A