Recently I am trying to use androidx.work.*
stuff to perform background tasks. It works fine with the MVVM pattern. However, I met several issues when using the Worker(via OneTimeWorkRequest.Builder) to replace the AsyncTask.
First, how to pass out generic object result back from the Worker? It seems only primitive types are supported. What if I want to decode the image file in the worker thread and pass the decoded drawable or bitmap back to the main thread observer? I have a workaround is to put the object in some global data store but it does not seem to be a good practice.
Another problem is that when I try to observe on the live data obtained via WorkManager.getWorkInfosByTagLiveData(TAG_NAME)
, it will also return the former en-queued worker info to me. So I have to call WorkManager.pruneWork()
to avoid this issue. And this does not seem to be a good practice either.
Thanks for your advice.