Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager - how to execute jobs sequentially

I have a service listening for incoming FCM notifications. When such a notification is received, I run a job with WorkManager.

When multiple notifications are received at the same time, how can I ensure that only one job is executed at the time? The jobs should be executed sequentially. In this case, I want to send an sms and these cannot not be sent simultaneously. (Note that I do other stuff like http requests in this job before and after sending sms, that's why I decided to create a job for it instead of sending an sms straight from the service.)

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //...
        OneTimeWorkRequest sendSmsWorker = new OneTimeWorkRequest.Builder(SendSmsWorker.class).build();
        WorkManager.getInstance().enqueue(sendSmsWorker);
        //...
    }
}

I've checked the advanced section in the WorkManager docs; it mentions Chained sequences, however these must be chained to each other explicitly (beginWith/then/then/...).

like image 826
user2923322 Avatar asked Oct 14 '18 13:10

user2923322


People also ask

What is the difference between job scheduler and WorkManager in Android?

As mentioned in the previous section, WorkManager internally uses the JobScheduler API on Android 6.0 (API Level 23 – Marshmallow) and above devices. Therefore, there won't be any major differences between the WorkManager and JobScheduler on Android 6.0 and above devices.

How does a WorkManager work?

WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

How do you implement a WorkManager?

To get started using WorkManager, first import the library into your Android project. Once you've added the dependencies and synchronized your Gradle project, the next step is to define some work to run.

What is difference between WorkManager and service?

WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.


1 Answers

You need to create the jobs as a OneTimeWorkRequest with the same group name, and enqueue them as a unique work with APPEND. This will make all the jobs to be appended to the same chain and execute sequentially.

Beware that you should always return Result.SUCCESS, or Result.RETRY at most. Returning Result.FAILURE will cancel all the enqueued jobs. If you need to know if a job has failed, then you could set a custom flag in the worker setOutputData to handle such scenario as you may require.

final String JOB_GROUP_NAME = "your_jobs_group_name";

......

OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(YOUR_WORK.class)
    .setInputData(YOUR_WORK_DATA_BUILDER.build())
    .build();

final WorkManager workManager = WorkManager.getInstance();
WorkContinuation work = workManager.beginUniqueWork(JOB_GROUP_NAME, ExistingWorkPolicy.APPEND, request);
work.enqueue();
like image 141
PerracoLabs Avatar answered Oct 10 '22 23:10

PerracoLabs