Search code examples
androidservicekotlin-coroutinesandroid-workmanager

Service vs WorkManager vs Kotlin Coroutines


I am mostly confused between Services and Coroutines

I don't understand why should i be using coroutines vs when should i be using Services? When i am downloading a file, do i need to use service or can i just use coroutine? Both of them can not survive process death.

And if i can download a file using coroutines, what do i need the service for at all?

Can you please list all of these and give some examples where i would want to use Service, where would i use Coroutines and where would i use WorkManager?

Also, should i be using AsyncTask at all? Because i read that it has many flaws in it and it can create memory leaks.

I also read this article about background work: https://developer.android.com/guide/background

but it talks about only Workmanager and Coroutines but no mention of services. Why?


Solution

  • Background Work

    https://developer.android.com/guide/background

    In Android and any UI application, you have the Main Thread. If you do something heavy on the Main Thread you will block the user's interaction. So you need to do such work in another Thread. You can do this on Android using:

    • You create a Thread
    • Executor with a pool of Threads
    • Handler
    • AsyncTask
    • RxJava
    • Coroutines

    App lifecycle

    https://developer.android.com/guide/components/activities/process-lifecycle

    But the Android application lifecycle is a complicated thing. You can't know when an application will die. It is a process that consumes some amount of resources. When the OS has no resources it will kill some applications. So you have priorities and based on them - the application is less likely to be killed. And they are part of the above link. As you can guess - if the app is visible - it is highly unlikely to die. But what if you want to finish your work, but without keeping the user engaged. This is point 3 from the link:

    • You can use a Service

    The idea of the Services is that you can do something "in the background", but it does not mean a background Thread. It means that it is in the background for the user. The plain Services starts on the MainThread. There are specific implementations that has their own Background Thread.

    And also what a Service gives you - it will make your application less likely to be killed.

    WorkManager

    There are many APIs in Android. During the years they become more and more. But at the same time there are more and more restrictions - with the idea that the user's battery must be saved. Also the network resources. So it is hard to pick the right API, but at the same time workaround the restrictions.

    So WM was invented. It is for cases where you want your work to finish for sure at some point. Even on a device restart. But you don't care that much about timing. Uploading a video is the best example. If you want something with an exact period like every 1 hour for sure - You need to use AlarmManager. With WM it might be 1 hour, 1 hour and a half, 4 hours, 6 hours, 1 hour periods. And for the video - it might start now, you upload 30MBs and in 2 hours - the other 70Mbs.

    You should use a Service when you are actively tracking or engaging with the user. Like for example Music applications, Running applications, etc.

    And WM is something that underneath is using a Service, Broadcast Receivers, Room, etc. It hides a lot of complexity for you.