Asynchronous Worker in Android WorkManager

I used a countdownlatch and waited for this to reach 0, which will only happen once the asynchronous callback has updated it. See this code: public WorkerResult doWork() { final WorkerResult[] result = {WorkerResult.RETRY}; CountDownLatch countDownLatch = new CountDownLatch(1); FirebaseFirestore db = FirebaseFirestore.getInstance(); db.collection(“collection”).whereEqualTo(“this”,”that”).get().addOnCompleteListener(task -> { if(task.isSuccessful()) { task.getResult().getDocuments().get(0).getReference().update(“field”, “value”) .addOnCompleteListener(task2 -> { if (task2.isSuccessful()) … Read more

Check if WorkManager is scheduled already

Update If you need to check already running work manager just because you don’t want duplicate works. You can simply use enqueueUniquePeriodicWork() This method allows you to enqueue a uniquely-named PeriodicWorkRequest, where only one PeriodicWorkRequest of a particular name can be active at a time. For example, you may only want one sync operation to … Read more

How to create a Worker with parameters for in WorkManager for Android?

You can use setInputData method to send data just like Bundle. /*** Logic to set Data while creating worker **/ val compressionWork = OneTimeWorkRequest.Builder(CompressWorker::class.java) val data = Data.Builder() //Add parameter in Data class. just like bundle. You can also add Boolean and Number in parameter. data.putString(“file_path”, “put_file_path_here”) //Set Input Data compressionWork.setInputData(data.build()) //enque worker WorkManager.getInstance().enqueue(compressionWork.build()) /*** … Read more

How to solve ‘Program type already present: com.google.common.util.concurrent.ListenableFuture’?

In my case, I had to add the following configurations to app’s module build.gradle: configurations { all*.exclude group: ‘com.google.guava’, module: ‘listenablefuture’ } It happens because some dependencies use com.google.guava:guava and com.google.guava:listenablefuture together. It causes a dependency conflict.

Android Work Manager vs Services?

WorkManager comes with following features: Provides tasks which can survive process death It can waken up the app and app’s process to do the work thereby guarantees that works will be executed. Allows observation of work status and the ability to create complex chains of work Allows work chaining which allows to segregate big chunk … Read more

Schedule a work on a specific time with WorkManager

Unfortunately, you cannot schedule a work at specific time as of now. If you have time critical implementation then you should use AlarmManager to set alarm that can fire while in Doze to by using setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). You can schedule a work, with onetime initial delay or execute it periodically, using the WorkManager as … Read more