JobPropertyDelegate
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()
Content copied to clipboard
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
}
Content copied to clipboard