I'm new to the new architecture component WorkManager, I do my API calls via Retrofit and RxJava.
My use case here is to get new posts from the Backend, then show notification, and update a widget.
So the code inside doWork() method from the Worker class, may look like something like this.
@NonNull @Override public Result doWork() { AppDependencies appDependencies = new AppDependencies((Application) getApplicationContext()); Repository repository = appDependencies.getRepository(); repository.getNewPosts() .flatMap(newPosts -> repository.inserPosts(newPosts).toObservable()) .doOnError(Timber::e) //if success - > return Result.SUCCESS, // -> show notification // -> update widget // error-> return Result.Failure .dontKnowWhatBestNextThing; //blocking or subscribing //if we reached here then Retry return Result.RETRY; }
My Question is what is the right way to use a RxJava code inside the Worker Class because the doWork() method has a return value, so Do I have to make Rx code Synchronous.
if I'm using the nonblocking Rx approach, how can I return value (Success - Failure - Retry)
What is Work Manager? Work Manager is a library part of Android Jetpack which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or device restarts i.e. even your app restarts due to any issue Work Manager makes sure the scheduled task executes again.
Use WorkManager for reliable workWorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.
ListenableWorker is the base class for Worker , CoroutineWorker , and RxWorker . It is intended for Java developers who have to interact with callback-based asynchronous APIs such as FusedLocationProviderClient and are not using RxJava.
Another nice feature of WorkManager is that it respects power-management features so that if a job is scheduled to run at a defined time and the device is in Doze at that time, WorkManager will try to run the task during a maintenance window if the constraints are met or after Doze is lifted.
Since WorkManager version 1.0.0-alpha12
they added a new artifact called work-rxjava2
that includes RxWorker
class exactly for this purpose. It is a special case of ListenableWorker
expecting Single<Result>
.
To implement it, first make sure you include correct artifacts to your build.gradle
:
dependencies { ... implementation "android.arch.work:work-runtime-ktx:$work_version" implementation "android.arch.work:work-rxjava2:$work_version" }
And implement your RxWorker
:
class MyRxWorker(context : Context, params : WorkerParameters) : RxWorker(context, params) { val remoteService = RemoteService() override fun createWork(): Single<Result> { return remoteService.getMySingleResponse() .doOnSuccess { /* process result somehow */ } .map { Result.success() } .onErrorReturn { Result.failure() } } }
Edit: WorkManager now officially supports an RxWorker
. Take a look at the answer above for more information.
doWork
happens on a background thread. So it's safe to block. You should wait for the Observable
to complete before you return a Result
.
We are also working on making this easier with asynchronous APIs. Stay tuned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With