Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run workmanager immediately

Tags:

android

In my android app i have a functionality by which a user can sync data (saved in sqlite) by pressing Sync button some where in Drawer menu. On click of sync button i starts the service and data gets synced to server.

But now i am using work manager to sync data. My question is how can we start work manager immediately on a button click.

I am creating Onetime request. some time it triggers but not other times.

like image 784
sanjay Avatar asked Nov 14 '18 10:11

sanjay


People also ask

How do I force a task to run immediately in workmanager?

In doWork () you will also tell WorkManager that the task should be run immediately and in a foreground service. To do this, you have to create a ForegroundInfo object and provide it to setForeground (). ForegroundInfo takes a notification id as well as the Notification that will be displayed as parameters.

What is the use of workmanager?

Give a warm welcome to WorkManager. WorkManager is a library that makes it easy to schedule deferrable, asynchronous tasks even if the app exits or the device restarts. It was designed to be backwards compatible to API 14 and does so by wrapping JobScheduler, AlarmManager, and BroadcastReceivers all in one.

How to implement workmanager in your Android app?

Follow the below steps to implement WorkManager into your Android app. 1. Adding the Gradle dependencies Add the following dependencies in your app’s build.gradle file: 2. Create a background task using the Worker class The work is defined inside the dowork () method of the Worker class.

When to use workmanager while working with background processing?

When to use WorkManager while working with background processing? Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately.


4 Answers

WorkManager 2.7.0 introduces the concept of expedited work. Expedited work starts immediately and complete within a few minutes. here the doc

OneTimeWorkRequestBuilder<T>().apply {
    setInputData(inputData) 
 setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}.build()
like image 132
nAkhmedov Avatar answered Oct 16 '22 09:10

nAkhmedov


Upd: The right way is to separate your logic from worker and call it from worker or from UI as needed. As Nurseyit pointed WM is not for immediate tasks. Also there is RxWorker from android.arch.work:work-runtime-ktx see docs which can easily wrap Rx network request or facade.

This can be used as quickfix or coding experiment: You can cancel enqueued Work by button and enqueue it again next line. Work Manager will try to launch this new Worker as soon as possible. While button is pressed app is foreground so there is a good chance that Work will run immediately. This way also will not clutter Worker's by extracting task logic to separate class, checking that in-Worker task instance is not running, cancelling, waiting for immediate task, etc.

like image 41
MainActivity Avatar answered Oct 16 '22 08:10

MainActivity


the documentation says that workManager is just for deferrable tasks. So we can't expect from it immediate execution. Maybe in some cases it will run immediately but there is no guaranty that it will do always. You should use coroutines or executors.

here is the answers:

can I always use WorkManager instead of coroutines?

https://www.youtube.com/watch?v=pe_yqM16hPQ

like image 43
Nurseyit Tursunkulov Avatar answered Oct 16 '22 07:10

Nurseyit Tursunkulov


Here and here are "advanced" WorkManager guide/ medium post which goes over this.

In July 2020, Ben Weiss (Android Developer Relations at Google) said

We recommend you use WorkManager to execute long running immediate tasks. / Just make sure to make it a foreground service by using setForeground/ setForegroundAsync.

This specifies that the WorkRequest is long-running or otherwise important. In this case, WorkManager provides a signal to the OS that the process should be kept alive if possible while this work is executing.

docs

Calls to setForegroundAsync must complete before a ListenableWorker signals completion by returning a ListenableWorker.Result.

Under the hood, WorkManager manages and runs a foreground service on your behalf to execute this WorkRequest, showing the notification provided in ForegroundInfo.

like image 33
Ben Butterworth Avatar answered Oct 16 '22 07:10

Ben Butterworth