Monday 14 January 2019

Android Architecture Components : Work Manager (Introduction)

Hi All ,

Continuing the series of Android Architecture Components , I am going to share one more topic i.e. work manager . 

First , 

What is Work Manager :

WorkManager is part of Android Jetpack and an Architecture Component for background work that needs a combination of opportunistic and guaranteed execution. 
Opportunistic execution means that WorkManager will do your background work as soon as it can. Guaranteed execution means that WorkManager will take care of the logic to start your work under a variety of situations, even if you navigate away from your app.


Work Manger attributes :

WorkManager is a simple, but incredibly flexible library that has many additional benefits. These include:
  • Support for both asynchronous one-off and periodic tasks
  • Support for constraints such as network conditions, storage space, and charging status
  • Chaining of complex work requests, including running work in parallel
  • Output from one work request used as input for the next
  • Handles API level compatibility back to API level 14(see note)
  • Works with or without Google Play services
  • Follows system health best practices
  • LiveData support to easily display work request state in UI

Why Work manager-

For background task which needs guaranteed execution and can be deferrable we have lot of options to do.We can use JobScheduler API but it is supported for API≥21 to overcome this we have FirebaseJobDispatcher library which provide backward compatibility upto API 14 but it requires Google Play services.


So to avoid all these handling Work manager comes to rescue.
You don’t need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option.It is wrapper on all above concepts.



Work Manager Uses :

WorkManager sits on top of a few APIs such as JobScheduler, FirebaseJobDispatcher and AlarmManager

WorkManager chooses the appropriate way to run your task based on such factors as the device API level and the app state. If WorkManager executes one of your tasks while the app is running, WorkManager can run your task in a new thread in your app's process. If your app is not running, WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager. You don't need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option.
The WorkManager library is a good choice for tasks that are useful to complete, even if the user navigates away from the particular screen or your app.
Some examples of tasks that are a good use of WorkManager:
  • Uploading logs
  • Applying filters to images and saving the image
  • Periodically syncing local data with the network
WorkManager offers guaranteed execution, and not all tasks require that. As such, it is not a catch-all for running every task off of the main thread.

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...