13

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.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
sanjay
  • 228
  • 2
  • 12
  • You can use a background service instead of Workmanager – Harish Jose Nov 14 '18 at 10:30
  • @HarishJose i can not use service. Because i also want to run sync process every 20 min (even if app is in background) . if i use service than i will have to make it a foreground service for updated android version (api 26 or above). – sanjay Nov 14 '18 at 10:34
  • Yeah you're right. So why don't you try Workmanager with very less time(say schedule to few seconds from now)? – Harish Jose Nov 14 '18 at 10:38
  • 1
    @HarishJose we can not set Workmanager to run less than 15 min – sanjay Nov 14 '18 at 10:42

4 Answers4

9

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()
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
3

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.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
1

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.

MainActivity
  • 1,650
  • 2
  • 12
  • 16
1

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

Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78