lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

SynchronousWorkManager

public interface SynchronousWorkManager

androidx.work.SynchronousWorkManager


Blocking methods for WorkManager operations. These methods are expected to be called from a background thread.

Summary

Public methods

abstract void cancelAllWorkByTagSync(String tag)

Cancels all unfinished work with the given tag in a synchronous fashion.

abstract void cancelUniqueWorkSync(String uniqueWorkName)

Cancels all unfinished work in the work chain with the given name in a synchronous fashion.

abstract void cancelWorkByIdSync(UUID id)

Cancels work with the given id in a synchronous fashion if it isn't finished.

abstract void enqueueSync(WorkRequest... workRequest)

Enqueues one or more WorkRequest in a synchronous fashion.

abstract void enqueueSync(List<? extends WorkRequest> workRequest)

Enqueues the List of WorkRequest in a synchronous fashion.

abstract void enqueueUniquePeriodicWorkSync(String uniqueWorkName, ExistingPeriodicWorkPolicy existingPeriodicWorkPolicy, PeriodicWorkRequest periodicWork)

This method allows you to synchronously enqueue a uniquely-named PeriodicWorkRequest, where only one PeriodicWorkRequest of a particular name can be active at a time.

abstract WorkStatus getStatusByIdSync(UUID id)

Gets the WorkStatus of a given work id in a synchronous fashion.

abstract List<WorkStatus> getStatusesByTagSync(String tag)

Gets the WorkStatus for all work with a given tag in a synchronous fashion.

abstract List<WorkStatus> getStatusesForUniqueWorkSync(String uniqueWorkName)

Gets the WorkStatus for all work for the chain of work with a given unique name in a synchronous fashion.

Public methods

cancelAllWorkByTagSync

public abstract void cancelAllWorkByTagSync (String tag)

Cancels all unfinished work with the given tag in a synchronous fashion. Note that cancellation is dependent on timing (for example, the work could have completed in a different thread just as you issue this call). Use getStatusByIdSync(UUID) to find out the actual state of the work after this call. This method is expected to be called from a background thread.

Parameters
tag String: The tag used to identify the work

cancelUniqueWorkSync

public abstract void cancelUniqueWorkSync (String uniqueWorkName)

Cancels all unfinished work in the work chain with the given name in a synchronous fashion. Note that cancellation is dependent on timing (for example, the work could have completed in a different thread just as you issue this call). Use getStatusByIdSync(UUID) to find out the actual state of the work after this call. This method is expected to be called from a background thread.

Parameters
uniqueWorkName String: The unique name used to identify the chain of work

cancelWorkByIdSync

public abstract void cancelWorkByIdSync (UUID id)

Cancels work with the given id in a synchronous fashion if it isn't finished. Note that cancellation is dependent on timing (for example, the work could have completed in a different thread just as you issue this call). Use getStatusByIdSync(UUID) to find out the actual state of the work after this call. This method is expected to be called from a background thread.

Parameters
id UUID: The id of the work

enqueueSync

public abstract void enqueueSync (WorkRequest... workRequest)

Enqueues one or more WorkRequest in a synchronous fashion. This method is expected to be called from a background thread and, upon successful execution, you can rely on that the work has been enqueued.

Parameters
workRequest WorkRequest: The Array of WorkRequest

enqueueSync

public abstract void enqueueSync (List<? extends WorkRequest> workRequest)

Enqueues the List of WorkRequest in a synchronous fashion. This method is expected to be called from a background thread and, upon successful execution, you can rely on that the work has been enqueued.

Parameters
workRequest List: The List of WorkRequest

enqueueUniquePeriodicWorkSync

public abstract void enqueueUniquePeriodicWorkSync (String uniqueWorkName, 
                ExistingPeriodicWorkPolicy existingPeriodicWorkPolicy, 
                PeriodicWorkRequest periodicWork)

This method allows you to synchronously 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 be active. If there is one pending, you can choose to let it run or replace it with your new work. This method is expected to be called from a background thread. The uniqueWorkName uniquely identifies this PeriodicWorkRequest.

Parameters
uniqueWorkName String: A unique name which for this operation

existingPeriodicWorkPolicy ExistingPeriodicWorkPolicy: An ExistingPeriodicWorkPolicy

periodicWork PeriodicWorkRequest: A PeriodicWorkRequest to enqueue. REPLACE ensures that if there is pending work labelled with uniqueWorkName, it will be cancelled and the new work will run. KEEP will run the new PeriodicWorkRequest only if there is no pending work labelled with uniqueWorkName.

getStatusByIdSync

public abstract WorkStatus getStatusByIdSync (UUID id)

Gets the WorkStatus of a given work id in a synchronous fashion. This method is expected to be called from a background thread.

Parameters
id UUID: The id of the work

Returns
WorkStatus A WorkStatus associated with id

getStatusesByTagSync

public abstract List<WorkStatus> getStatusesByTagSync (String tag)

Gets the WorkStatus for all work with a given tag in a synchronous fashion. This method is expected to be called from a background thread.

Parameters
tag String: The tag of the work

Returns
List<WorkStatus> A list of WorkStatus for work tagged with tag

getStatusesForUniqueWorkSync

public abstract List<WorkStatus> getStatusesForUniqueWorkSync (String uniqueWorkName)

Gets the WorkStatus for all work for the chain of work with a given unique name in a synchronous fashion. This method is expected to be called from a background thread.

Parameters
uniqueWorkName String: The unique name used to identify the chain of work

Returns
List<WorkStatus> A list of WorkStatus for work in the chain named uniqueWorkName