```coroutineScope { launch { while (isActive) {...
# coroutines
u
Copy code
coroutineScope {
	launch {
		while (isActive) { <--------
            
        }
	}
	...
}

coroutineScope {
	launch {
		while (true) { <---------
            
        }
	}
	...
}
When I want to do something while the coroutine is not cancelled - should I use
isActive
or
true
? Both seem to work
y
Depends on the code you have inside.
isActive
is probably safer.
u
Why? say im sending some stuff to a channel
s
If the code in your while-loop never calls any suspend functions, then while(true) will never end
u
I see. So, do you use it? I often don't see it even in first party samples. Somehow it seems like a unnecessary foot gun
j
You most often don't need an explicit
isActive
check because you usually call suspend functions inside the loop. If you don't, the correct fix is probably to add some
yield()
calls (or otherwise introduce suspension) rather than to change the condition
k
If the call inside the while loop blocks the calling thread then you'll need to introduce another mechanism to cooperatively cancel.
u
so...
while(isActive)
is the better practise it seems
j
No, not blocking is the better practice
@kevin.cianfarini yes, and usually also to suspend, like
yield()
. So you don't need
isActive
checks
s