- Developer's Commute
- Posts
- Do you want to understand WorkManager ? Here are the foundations of WorkManager
Do you want to understand WorkManager ? Here are the foundations of WorkManager
In this newsletter we are exploring the working and behind the scenes of the Work Manager.
Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today we have Core Android Concepts, some interesting tweets, articles, motivation and in the end, Coding Motivation to keep you moving We hope that you enjoy this ride with us and you have a pleasant journey ahead.
Android Core Concepts
Exploring Android Core Concepts
Explaining the Work Manager
Have you ever wondered, how an app uploads your image to the server even if it is closed by the user?
If an app is killed by the Android OS due to a lack of memory can I still upload my image?
It is an interesting question to think about.
How does an app upload or update something even when it is closed by the user or OS?
The answer is simple: Work Manager
But there is more to a work manager than meets the eye.
In today's newsletter, we explore more about work managers and try to find their inner workings. Let’s get started with the definition of a Work Manager.
Work Manager is an Android Jetpack Library that allows developers to run deferrable guaranteed tasks in the background when certain constraints are met. What do we exactly mean by that?
Let's try to understand its definition and when to use workmanager.
→ It is used for guaranteed deferrable tasks:
Deferrable means that the work manager is used for the tasks that do not require immediate execution, instead, they can be carried out later on.
Guaranteed means that these tasks are important to execute so they need to be executed no matter what.
Let me give you an example: If you are trying to upload an image on Instagram and suddenly you receive a call. The Instagram app will go in the background. Now it is possible that the app gets closed by Android OS or stays in the background. If it gets closed by the Android OS, then whenever the user opens the app, it will resume uploading the image. If it is available in the background then the app will continue uploading the image in the background.
In one case the tasks get deferred to whenever the user opens the app whereas in the other case, the tasks will continue in the background. The ultimate goal is to upload the image, which is guaranteed. But, it can be deferred if it is not possible at this exact moment.
→ When constraints are met:
The background tasks will only continue if the constraints are met.
We do not need to keep on uploading the image if the internet is not available. As soon as the internet becomes available we will continue the task. So the internet here is a constraint.
→ When to use WorkManager:
Here is an interesting image from this blog post that might help you understand the concept of work manager better.
If you are running a background task that is locally deferrable work and sync then use the Work Manager
If you are running a background task that is triggered online use Firebase Cloud Messaging and Work Manager
If you are running a task that is a high priority then run it as a foreground services
If the task needs to be set at an exact time then use Alarm Manager
Benefits of using Work Manager:
Handles compatibility with different OS versions
Follows system health
Supports asynchronous tasks
Supports chained tasks
Lets you set constraints
Guarantees execution of tasks
How does the Work Manager work with different OS versions?
→ If the Work manager finds API 23+ then it uses a job scheduler else it uses a Broadcast receiver with Alarm Manage
How do the internals of a Work Manager work?
→ Internal Task Executor is a single-threaded executor that handles all the tasks.
→ WorkManager Database is a database that contains all the information on the tasks and keeps track of their status. It includes all input outputs and constraints. It is what guarantees that the task will be executed even if the app is closed.
→ WorkFactory creates these workers.
→ Default executor that runs the task on the main thread until you specify otherwise.
hen your work request enqueues the work it goes to the internal task executor. The internal task executor saves the database. When constraints are met, the internal task executor tells the work manager to produce a worker. The work executes dowork() on the main thread. That is how tasks are executed.
In the next newsletter, we will try to code our work manager. For now, this is for the newsletter. I hope you enjoyed this newsletter. If I have made some mistake. Please forgive me.
Tweets That You Might Love
Exploring Interesting Tweets
Tweets That You Will Find Interesting
Do you want to learn about Kotlin Flows?
Here is an interesting tweet for you →
We use suspend functions in coroutines to produce an output of long-running tasks.
Suspend functions are the types of functions that can be suspended.
It means that we can stop the function for a while and resume it whenever we want.
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
3:54 PM • Nov 6, 2023
Do you want to learn about the Ktor Client side?
Then you might find this tweet interesting→
Breaking down Ktor for Android Developers:
→ Ktor is a multiplatform Asynchronous HTTP client which uses different plugins to extend its functionalities
→ We first build an HTTP client and then add plugins to it. That forms the basis of Ktor.
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
5:57 AM • Nov 4, 2023
Do you want to understand OAuth?
Here is something interesting for you→
Understanding OAuth
OAuth is an open standard that allows users to grant limited access to their data on one site to other sites or applications without exposing their passwords. It has become the backbone of secure authorization across the web and mobile apps.
The OAuth… twitter.com/i/web/status/1…
— Sahn Lam (@sahnlam)
6:11 AM • Nov 8, 2023
ArticlesThat You Might Love
Exploring Interesting Articles
Articles That You Will Find Interesting
Do you want to understand the difference between Kotlin Coroutines and Kotlin Flows?
Here is our Newsletter that you might want to check out →
Do you want to understand Dagger Hilt?
Here is an interesting article for you →
Developer Motivation
What is your Stratergy?