https://kotlinlang.org logo
#feed
Title
# feed
d

Dan Newton

05/12/2020, 8:07 AM
A continuation of the getting started with coroutines series i have been writing - https://lankydan.dev/cancelling-coroutines
👍 1
s

Stephan Schroeder

05/12/2020, 10:46 AM
pretty sure it should be
false
here:
Copy code
Cancelling a coroutine will change a job's isActive flag to true.
d

Dan Newton

05/12/2020, 10:46 AM
ah indeed, thx for pointing that out
s

Stephan Schroeder

05/12/2020, 11:03 AM
this is nitpicking but instead of if-blocking the whole
Copy code
for (it in 0..1000) {
      if (isActive) {
        Thread.sleep(50)
        println("I am still going..")
      }
    }
i'd suggest to just break the loop
Copy code
for (it in 0..1000) {
      if (!isActive) break
      Thread.sleep(50)
      println("I am still going..")
    }
it saves on line, doesn't iterate over the rest of all numbers once the coroutine is canceled and doesn't indent the rest of the statement.
ah, ok. you kind of do that with your next return-way of canceling 😅
d

Dan Newton

05/12/2020, 11:04 AM
I was literally just about to mention that
maybe it suggests that the for loop one should be removed as an example
thx for your feedback 👍
s

Stephan Schroeder

05/12/2020, 11:13 AM
thx for your blog post! Canceling coroutines was literally the next item on my todo-list after finishing the Coroutines Hands-on course (which tells you about cancel, but not about it's cooperative nature).
2 Views