Sergio C.
12/03/2023, 3:30 PMcoroutineTest
function is called from compose and with each button click the id is increased, and the loop needs to run alone, no other loops should run at the same time.
I would expect the while loop to stop when vmJob?.cancel is called, but if I click many times, and it doesn't need to be very fast, I can see multiple id in the logcat meaning that multiple loops are running at the same time.
Ok I've tried cancelAndJoin, Mutexes, Semaphores, AtomicBooleans, ensureActive, hashmaps to store the jobs, and I still can run multiple while loops at once!
What a heck is going on? 🤷
var vmJob: Job? = null
override fun coroutineTest(id: Int) {
vmJob?.cancel()
vmJob = viewModelScope.launch(Dispatchers.IO) {
while (isActive) {
delay(1000)
println("coroutineTest: $id")
}
}
}
asdf asdf
12/03/2023, 4:57 PMcancelAndJoin
instead of cancel
?Sergio C.
12/03/2023, 4:59 PMSergio C.
12/03/2023, 5:01 PM