Kenenisa Bekele
Kenenisa Bekele

Reputation: 845

Cancel a Job that runs a coroutine and reuse the job object

I currently have a job that executes some process (it is a bit cpu intensive takes about 10 seconds)

But I would like to cancel it immediately when the next call to executeProcess() comes.

private var myJob: Job? = null

fun executeProcess() {
    myJob?.cancel()
    myJob = scope.launch(Dispatchers.IO) {
        //doCPUIntensiveTask()
        //This is always Active!
        println("isActive $isActive ")
    }
}

How is this possible if I want to reuse the same job object? if not possible, Is there any alternative solution?

Upvotes: 1

Views: 650

Answers (1)

Upvotes: 1

Related Questions