Schedule tasks with WorkManager

What is WorkManager

WorkManager is a library used to enqueue deferrable, asynchronous tasks that is guaranteed to execute sometime after its Constraints are met.
WorkManager allows observation of work status and the ability to create complex chains of work.
This API allows you to schedule jobs (one-off or repeating) and chain.

WorkManager is a simple, but incredibly flexible library that has many additional benefits.

1.Support for both asynchronous one-off and periodic tasks
2.Support for constraints such as network conditions, storage space, and charging status
3.Chaining of complex work requests, including running work in parallel
4. Supports chained tasks with input/output, one work request used as input for the next
5. Handles API level compatibility back to API level 14.
6.Works with or without Google Play services
7.Follows system health best practices
8. Guarantees task execution, even if the app or device restart

When to use WorkManager

1. Periodically syncing local data with the network
2.A task initiated in response to an external event (FCM + WorkManager)

WorkManager classes

Worker: Worker have only one method to implement doWork() which is executed on a background thread. It’s the place where all your background tasks should be done

WorkRequest: This represents a request to do some work. You’ll pass in your Worker as part of creating your WorkRequest.
When making the WorkRequest you can also specify things like Constraints(e.g., internet, charging ) on when the Worker should run.

WorkManager: This class actually schedules your WorkRequest and makes it run.

WorkResult: Success, Failure, Retry.

Data: Persistable set of key/value pairs which are passed to/from Worker.

 

 

References:

. . .

Comments (2)

Add Your Comment

  • Naina
    Wow, What a blog.
    • yogendra singh
      Thanks for the appreciation Naina.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close