Site icon C1CTech

Android JetPack WorkManager Example

<p>Google released <a href&equals;"https&colon;&sol;&sol;android-developers&period;googleblog&period;com&sol;2018&sol;05&sol;use-android-jetpack-to-accelerate-your&period;html" data-href&equals;"https&colon;&sol;&sol;android-developers&period;googleblog&period;com&sol;2018&sol;05&sol;use-android-jetpack-to-accelerate-your&period;html"><strong><span style&equals;"color&colon; &num;008000&semi;">Android Jetpack<&sol;span><&sol;strong><&sol;a>&period; It’s a set of libraries&comma; tools and architectural guidance to help make it quick and easy to build great Android apps&period; In this Android Jetpack&comma; team at Google release one library specifically designed for scheduling and managing the background tasks&period; It’s called &OpenCurlyDoubleQuote;<a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;topic&sol;libraries&sol;architecture&sol;workmanager" data-href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;topic&sol;libraries&sol;architecture&sol;workmanager"><strong><span style&equals;"color&colon; &num;008000&semi;">WorkManager<&sol;span><&sol;strong><&sol;a>”&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>WorkManager<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">WorkManager<&sol;span><&sol;strong> API makes it easy to specify deferrable&comma; asynchronous tasks and when they should run&period; These APIs let you create a task and hand it off to WorkManager to run immediately or at an appropriate time&period; For example&comma; an app might need to download new resources from the network from time to time&period; Using these classes&comma; you can set up a task&comma; choose appropriate circumstances for it to run &lpar;like &&num;8220&semi;only while device is charging and online&&num;8221&semi;&rpar;&comma; and hand it off to WorkManager to run when the conditions are met&period; The task is still guaranteed to run&comma; even if your app is force-quit or the device is rebooted&period;<&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Why need WorkManager<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>For background task which needs guaranteed execution and can be deferrable we have lot of options to do&period;We can use <strong><span style&equals;"color&colon; &num;008000&semi;">JobScheduler<&sol;span><&sol;strong> API but it is supported for API≥23&period; To overcome this we have <strong><span style&equals;"color&colon; &num;008000&semi;">FirebaseJobDispatcher<&sol;span><&sol;strong> library which provide backward compatibility upto API 14 but it requires Google Play services&period;So to avoid all these handling Work manager comes to rescue&period;<br &sol;>&NewLine;You don’t need to write device logic to figure out what capabilities the device has and choose an appropriate API&semi; instead&comma; you can just hand your task off to <strong><span style&equals;"color&colon; &num;008000&semi;">WorkManager<&sol;span><&sol;strong> and let it choose the best option&period;<&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">How WorkManager works<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>WorkManager chooses the appropriate way to run your task based on such factors as the device API level and the app state&period; If WorkManager executes one of your tasks while the app is running&comma; WorkManager can run your task in a new thread in your app&&num;8217&semi;s process&period; If your app is not running&comma; WorkManager chooses an appropriate way to schedule a background task depending on the device API level and included dependencies&comma; WorkManager might use <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;job&sol;JobScheduler&period;html">JobScheduler<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;firebase&sol;firebase-jobdispatcher-android&num;user-content-firebase-jobdispatcher-">Firebase JobDispatcher<&sol;a>&comma; or <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;AlarmManager&period;html">AlarmManager<&sol;a>&period;<&sol;p>&NewLine;<ol>&NewLine;<li>For devices whose API&gt&semi;&equals;23 it will use <strong><span style&equals;"color&colon; &num;008000&semi;">JobScheduler<&sol;span><&sol;strong> as the best option to schedule the task&period;<&sol;li>&NewLine;<li>For devices whose API&gt&semi;&equals;14 and in which google play services installed&comma; it will use <strong><span style&equals;"color&colon; &num;008000&semi;">FirebaseJobDispatcher<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>For devices whose API&lt&semi;14 it will use <strong><span style&equals;"color&colon; &num;008000&semi;">AlarmManager<&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;008000&semi;">BroadcastReceivers<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>FEATURES OF WORKMANAGER<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong style&equals;"font-style&colon; inherit&semi;">Guaranteed&comma; constraint-aware execution &colon; <&sol;strong><&sol;span>Let’s say If I wanna upload a photo&period; I only wanna do it when the device has the network&comma; that’s the constraint&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong style&equals;"font-style&colon; inherit&semi;">Respectful of system background restrictions &colon; <&sol;strong><&sol;span>Let’s say If your app in a doze mode it won’t wake up your phone just to do those work&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong style&equals;"font-style&colon; inherit&semi;">Backwork compatible with or without Google play services &colon;<&sol;strong><&sol;span> It uses internally <strong><span style&equals;"color&colon; &num;008000&semi;">AlarmManager<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">BroadcatsReceiver<&sol;span><&sol;strong> for device which don’t have google play services installed&period;<&sol;li>&NewLine;<li><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">Queryable <&sol;span>&colon; <&sol;strong>If you have a queue of work&comma; you can actually check what’s the state&period; Is it running right now&comma; has it succeeds or fails&period;<&sol;li>&NewLine;<li><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">Chainable <&sol;span>&colon; <&sol;strong>It is also chainable like you have work A depending on work B and C which in turn out a dependent on work D&period;<&sol;li>&NewLine;<li><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">Opportunistic <&sol;span>&colon; <&sol;strong>This means WorkManager tried to execute the work in your process as soon as the constraint match- without actually needing <span style&equals;"color&colon; &num;0000ff&semi;"><strong style&equals;"font-style&colon; inherit&semi;">JobScheduler<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;">WORKMANAGER CORE CLASSES<&sol;span><&sol;h4>&NewLine;<p>There are few WorkManager classes you need to know about&period;<&sol;p>&NewLine;<ul>&NewLine;<li><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">Worker<&sol;span> &colon;<&sol;strong> specifies what task you need to perform&period; The WorkManager APIs include an abstract <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;Worker&period;html">Worker<&sol;a><&sol;span><&sol;strong> You extend this class and perform the work here&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">WorkRequest<&sol;span> &colon; <&sol;strong>represents an individual task&period; A <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;WorkRequest&period;html">WorkRequest<&sol;a><&sol;span><&sol;strong> object specifies which Worker class should perform the task&period; However&comma; you can also add details to the WorkRequest object&comma; specifying things like the circumstances under which the task should run&period; Every WorkRequest has an autogenerated <strong><span style&equals;"color&colon; &num;008000&semi;">unique ID<&sol;span><&sol;strong>&semi; you can use the ID to do things like cancel a queued task or get the task&&num;8217&semi;s state&period; WorkRequest is an abstract class&semi; in your code&comma; you&&num;8217&semi;ll be using one of the direct subclasses&comma; <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;OneTimeWorkRequest">OneTimeWorkRequest<&sol;a><&sol;span><&sol;strong> or <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;PeriodicWorkRequest"><span style&equals;"color&colon; &num;008000&semi;">PeriodicWorkRequest<&sol;span><&sol;a>&period;<&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>          Types of WorkRequest<&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">           OneTimeWorkRequest<&sol;span> &colon; <&sol;strong>For task that you need to run only once&period;<&sol;p>&NewLine;<p><strong style&equals;"font-style&colon; inherit&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">           PeriodicWorkRequest<&sol;span> &colon; <&sol;strong>For task that you need to perform repeatedly&period;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>WorkManager &colon;<&sol;strong><&sol;span> enqueues and manages the work requests&period; You pass your WorkRequest object to <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;WorkManager&period;html">WorkManager<&sol;a><&sol;span><&sol;strong> to enqueue the task&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong> <&sol;strong><span style&equals;"color&colon; &num;000080&semi;"><strong>How to use WorkManager<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>So&comma; enough of this theory let’s see how we can integrate <span style&equals;"color&colon; &num;008000&semi;"><strong style&equals;"font-style&colon; inherit&semi;">WorkManager<&sol;strong><&sol;span> in Android app&period;<&sol;p>&NewLine;<p>First&comma; open your app level <strong><span style&equals;"color&colon; &num;008000&semi;">build&period; gradle<&sol;span><&sol;strong> file and add a dependency&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Add dependency<&sol;strong><&sol;span><strong> <&sol;strong><&sol;p>&NewLine;<pre><code>dependencies &lbrace;&NewLine;implementation "android&period;arch&period;work&colon;work-runtime&colon;1&period;0&period;0-alpha02" &rcub;<&sol;code><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Extend the worker class<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>Now the setup part is done&period; Let’s see how we can make a simple <strong><span style&equals;"color&colon; &num;008000&semi;">Worker<&sol;span>&period;<&sol;strong> <strong> <&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre><code>Class UploadImageWorker&colon; Worker&lpar;&rpar;  &lbrace;&NewLine;&NewLine;override fun doWork&lpar;&rpar;&colon; WorkerResult &lbrace;&NewLine;&NewLine;&sol;&sol; <strong><span style&equals;"color&colon; &num;008000&semi;">Upload image to FirebaseStorage<&sol;span><&sol;strong>&NewLine;uploadImage&lpar;&rpar;&NewLine;&NewLine;&sol;&sol; <strong><span style&equals;"color&colon; &num;008000&semi;">Indicate success or failure with your return value&colon;<&sol;span><&sol;strong>&NewLine;return WorkerResult&period;SUCCESS&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; &lpar;Returning RETRY tells WorkManager to try this task again&NewLine;&sol;&sol; FAILURE says not to try again&period;&rpar;<&sol;strong><&sol;span>&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">doWork<&sol;span> <&sol;strong>method actually runs on a background thread&period; WorkManager automatically runs this function on a background thread you don’t need to do run&period; You see <strong><span style&equals;"color&colon; &num;008000&semi;">doWork<&sol;span> <&sol;strong>method have return type <strong><span style&equals;"color&colon; &num;008000&semi;">WorkerResult<&sol;span>&period; <&sol;strong>So&comma; we need to return <span style&equals;"color&colon; &num;008000&semi;"><strong>Success<&sol;strong><&sol;span> if everything goes well and <strong><span style&equals;"color&colon; &num;008000&semi;">Failure<&sol;span> <&sol;strong>if any error occurred&period;<&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Create a  WorkRequest<&sol;span><&sol;strong><&sol;h5>&NewLine;<p>Now let’s see how we can call this <strong><span style&equals;"color&colon; &num;000080&semi;">UploadImageWorker<&sol;span><&sol;strong> class by creating request&period;<&sol;p>&NewLine;<pre><code>val uploadImageRequest &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;workManager&period;enqueue&lpar;uploadImageRequest&rpar;<&sol;code><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Adding Contraints<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">Constraints<&sol;span><&sol;strong> specifies restrictions on when the task should run &lpar;for example&comma; &&num;8220&semi;only when connected to the network&&num;8221&semi;&rpar;&period; You create the Constraints object with <strong><span style&equals;"color&colon; &num;008000&semi;">Constraints&period;Builder<&sol;span><&sol;strong>&comma; and pass the Constraints to the <strong><span style&equals;"color&colon; &num;008000&semi;">WorkRequest&period;Builder<&sol;span><&sol;strong> before creating the <strong><span style&equals;"color&colon; &num;008000&semi;">WorkRequest<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>Soon after this request enqueued it will start uploading images&period; Now&comma; what if you lose connectivity in the middle of this or even before this what if you do not have connectivity&period; You actually wanna constraints in this case&period;<&sol;li>&NewLine;<li>The following shows how to add constraints in <span style&equals;"color&colon; &num;008000&semi;"><strong>WorkRequest<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>val constraints &equals; Constraints&period;Builder&lpar;&rpar;&NewLine; &period;setRequiredNetworkType&lpar;NetworkType&period;CONNECTED&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;&NewLine;val uploadImageRequest &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;setConstraints&lpar;constraints&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;workManager&period;enqueue&lpar;uploadImageRequest&rpar;<&sol;code><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Observing Work<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>Now let’s say I want to observe the request&period; I wanna show a progress bar while this work is executing and I wanna hide the spinner when it’s done&period;<&sol;p>&NewLine;<p>The following shows how to observe a <strong><span style&equals;"color&colon; &num;008000&semi;">WorkRequest<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<pre><code>workManager&period;getStatusById&lpar;uploadImageRequest&period;id&rpar;&period;observe&lpar;this&comma; Observer &lbrace;&NewLine; if&lpar;it&excl;&equals;null &amp&semi;&amp&semi; it&period;state&period;isFinished&rpar;&NewLine; &lbrace;&NewLine; progressBar&period;setVisbility&lpar;GONE&rpar;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>it<&sol;strong> <strong>&colon;<&sol;strong><&sol;span> refers to <strong><span style&equals;"color&colon; &num;008000&semi;">WorkStatus<&sol;span><&sol;strong>  contains information about a particular task&period;<&sol;p>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">WorkManager<&sol;span><&sol;strong> provides a <strong><span style&equals;"color&colon; &num;008000&semi;">LiveData<&sol;span><&sol;strong> for each WorkRequest &period;The <span style&equals;"color&colon; &num;008000&semi;"><strong>LiveData<&sol;strong><&sol;span> holds a <strong><span style&equals;"color&colon; &num;008000&semi;">WorkStatus<&sol;span><&sol;strong> object&semi; by observing that <strong><span style&equals;"color&colon; &num;008000&semi;">LiveData<&sol;span><&sol;strong>&comma; you can determine the current status of the task&comma; and get any returned values after the task finishes&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;">Canceling a Task<&sol;span><&sol;h5>&NewLine;<p>You can cancel a task after you enqueue it&period; To cancel the task&comma; you need its work ID&comma; which you can get from the <strong><span style&equals;"color&colon; &num;008000&semi;">WorkRequest<&sol;span><&sol;strong> object&period; For example&comma; the following code cancels the <strong><span style&equals;"color&colon; &num;008000&semi;">uploadImageRequest<&sol;span><&sol;strong> &colon;<&sol;p>&NewLine;<pre><code>val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;workManager&period;cancelWorkById&lpar;uploadImageRequest&period;id&rpar;<&sol;code><&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Note<&sol;span><&sol;strong>&colon;-We can also set tag in work request using <strong><span style&equals;"color&colon; &num;008000&semi;">addTag<&sol;span><&sol;strong>&lpar;&rpar; method and can use this tag for query work status or cancelling the task instead of using <strong><span style&equals;"color&colon; &num;008000&semi;">workId<&sol;span><&sol;strong>&period;We can also set multiple tags on single request&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Running Multiple Request<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>Now let’s say I want to upload multiple images in parallel&period; The following shows how to execute requests in parallel&period;<&sol;p>&NewLine;<pre><code>val uploadImageRequest1 &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val uploadImageRequest2 &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val uploadImageRequest3 &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;workManager&period;enqueue&lpar;uploadImageRequest1&comma;uploadImageRequest2&comma;uploadImageRequest3&rpar;<&sol;code><&sol;pre>&NewLine;<p>These all work requests will be executed in parallel and everytime the order may differ&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;">Recurring tasks<&sol;span><&sol;h5>&NewLine;<p>You might have a task that you need to perform repeatedly&period; For example&comma; before uploading the list of images to firebase storage you want to  check whether the list of images is in the compressed form or not by calling <strong><span style&equals;"color&colon; &num;008000&semi;">compressCheckRequest<&sol;span><&sol;strong> every one hour&period;<&sol;p>&NewLine;<p>To create a recurring task&comma; use the <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;PeriodicWorkRequest&period;Builder&period;html">PeriodicWorkRequest&period;Builder<&sol;a><&sol;strong><&sol;span> class to create a <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;work&sol;PeriodicWorkRequest&period;html">PeriodicWorkRequest<&sol;a><&sol;span><&sol;strong> object&comma; then enqueue the <strong><span style&equals;"color&colon; &num;008000&semi;">PeriodicWorkRequest<&sol;span><&sol;strong> the same way you would a <strong><span style&equals;"color&colon; &num;008000&semi;">OneTimeWorkRequest<&sol;span><&sol;strong> object&period;<&sol;p>&NewLine;<pre><code>val compressCheckRequest &equals; PeriodicWorkRequest&period;Builder&lpar;CompressCheckWorker&colon;&colon;class&period;java&comma; 1&comma;&NewLine; TimeUnit&period;HOURS&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;workManager&period;enqueue&lpar;compressCheckRequest&rpar;<&sol;code><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;">Chained tasks<&sol;span><&sol;h5>&NewLine;<p>Your app might need to run several tasks in a particular order&period; <strong><span style&equals;"color&colon; &num;008000&semi;">WorkManager<&sol;span><&sol;strong> allows you to create and enqueue a work sequence that specifies multiple tasks&comma; and what order they should run in&period;<&sol;p>&NewLine;<p>For example&comma;here I will first compress the list of images  then upload it in firebase storage and then I will save the imageUrl in firebase database &period;The tasks must be run in the same order &period;<&sol;p>&NewLine;<pre><code>class CompressImageWorker &colon; Worker&lpar;&rpar; &lbrace;&NewLine; override fun doWork&lpar;&rpar;&colon; WorkerResult &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;compress image&NewLine; &sol;&sol;compressImage&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; return WorkerResult&period;SUCCESS&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class UploadImageWorker &colon; Worker&lpar;&rpar; &lbrace;&NewLine; override fun doWork&lpar;&rpar;&colon; Worker&period;WorkerResult &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;upload image to firebase storage&NewLine; &sol;&sol;uploadImage&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; return WorkerResult&period;SUCCESS&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;class SaveImageWorker &colon; Worker&lpar;&rpar; &lbrace;&NewLine; override fun doWork&lpar;&rpar;&colon; WorkerResult &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;save image to firebase database&NewLine; &sol;&sol;saveImage&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; return WorkerResult&period;SUCCESS&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;val compressImageRequest &equals; OneTimeWorkRequest&period;Builder&lpar;CompressImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;setConstraints&lpar;constraints&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val uploadImageRequest &equals; OneTimeWorkRequest&period;Builder&lpar;UploadImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;setConstraints&lpar;constraints&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;val saveImageRequest &equals; OneTimeWorkRequest&period;Builder&lpar;SaveImageWorker&colon;&colon;class&period;java&rpar;&NewLine; &period;setConstraints&lpar;constraints&rpar;&NewLine; &period;build&lpar;&rpar;&NewLine;&NewLine;val workManager &equals; WorkManager&period;getInstance&lpar;&rpar;&NewLine;&NewLine;workManager&period;beginWith&lpar;compressImageRequest&rpar;&period;then&lpar;uploadImageRequest&rpar;&period;then&lpar;saveImageRequest&rpar;&period;enqueue&lpar;&rpar;<&sol;code><&sol;pre>&NewLine;<p>I hope that this post helps you in understanding the basics of WorkManager&period;Thank You for being Here&period;&NewLine;

Exit mobile version