JobPropertyDelegate

class JobPropertyDelegate(job: Job? = null)

A property delegate for a Job that cancels the previous job if any, and sets the property to null once the job is completed using Job.invokeOnCompletion.

The implementation is thread-safe, meaning the property can be accessed and set from multiple threads.

Example usage:

private var job: Job? by JobPropertyDelegate()

This delegate is convenient when only one Job should be active for a given job property.

This delegate also helps to avoid incorrectly setting job to null at the end of the launch block. If the job completes immediately, the property might still be assigned and never become null.

// An example of possible incorrect behavior.
var job = launch {
// some code

// Might be called earlier than the result of the launch block is assigned to the variable.
job = null
}

Constructors

Link copied to clipboard
constructor(job: Job? = null)

Functions

Link copied to clipboard
operator fun getValue(thisRef: Any?, property: KProperty<*>): Job?
Link copied to clipboard
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Any?)