Android Work Manager vs Services?

25,623

WorkManager comes with following features:

  • Provides tasks which can survive process death
  • It can waken up the app and app's process to do the work thereby guarantees that works will be executed.
  • Allows observation of work status and the ability to create complex chains of work
  • Allows work chaining which allows to segregate big chunk of work into small works and execute them based on different constraints
  • Gracefully manages doze mode or other restrictions imposed by OS.

Following would be the cases where it would be helpful:

  • Executing long running background tasks like uploading media
  • Parsing and storing data in database.
  • Critical Tasks which needs to survive process deaths

Should i switch to work manager now considering the fact that i may have to write more intent services in the future?

In most cases it should be replacement for IntentService but you have to consider carefully before using it. It could be that IntentService was not the best choice at first place.

WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.

You really need to weigh whether you need capabilities of it before using it. Since Google is almost re-vamping the way we code, WorkManager would be solution of our Background processing woes. For sure it would be most important option since it abstracts handling of several constraints imposed by OS. You should consider using it for future implementations.

Also which option would help me test the code easily?

Google has also provided testing library which facilitates WorkManager's test at ease. Its still under development but should become more powerful before its released.

Share:
25,623
Dishonered
Author by

Dishonered

Hey whats up? Thanks for visiting my profile. I want to ask you , Why can't software engineers learn one technology and spend the rest of their lives peacefully? Why do they have to keep learning new things all the time? I think its unfair.

Updated on July 09, 2022

Comments

  • Dishonered
    Dishonered almost 2 years

    In my Android app i have multiple intent services which run one after another and the very first intent service is trigerred by a broadcast. I came across Work Manager a few days ago and really liked the simplicity of the Worker and the WorkManager classes. What are the pros and cons of Work Manager over regular intent services? Should i switch to work manager now considering the fact that i may have to write more intent services in the future? Also which option would help me test the code easily?

  • Mehdi Jahed Manesh
    Mehdi Jahed Manesh about 6 years
    Hi , since doWork() method is sync , is there any way to do some async work with RxJava or not ?
  • Sagar
    Sagar about 6 years
    @MehdiJahedManesh doWork() will be executed in background thread.
  • Nicolas Jafelle
    Nicolas Jafelle almost 6 years
    @Sagar let's say I want to do some periodic work in my app. I can directly use the PeriodicWorkRequest without worrying about if the process app is live or not? I mean WorkManager provides the same behaviour as a Service regarding continue even if the app/process is killed? Thanks!
  • Sagar
    Sagar almost 6 years
    @NicolasJafelle Yes. You can use PeriodicWorkRequest. The task is guaranteed to run, even if your app is force-quit or the device is rebooted
  • IgorGanapolsky
    IgorGanapolsky over 4 years
    Is there a reason to still use IntentService these days?
  • Anirudh Ganesh
    Anirudh Ganesh over 3 years
    What about critical uploads that are supposed to run for say every 5minutes? Because sometimes workers are paused if the user does not open the app for a very long time. In such a case, is it prudent to have services or workers?
  • GregHNZ
    GregHNZ about 2 years
    I'd like to add to this excellent answer that the android Data saver setting affects the behaviour. It seems (in my app) that work done by Work Manager is treated as background, and if Data saver is on, web-service calls from Work Manager tasks do not succeed.