https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

mgrazianodecastro

02/14/2023, 1:15 PM
if I have a long running task which I'm collecting with
Copy code
longRunningTask().collect{
   // ...
}
is there a way of canceling in the middle of the collecting? Like: I wan the function to finish itself and stop doing what it was doing
s

Sam

02/14/2023, 1:22 PM
You could consider using
transformWhile
which lets you return
true
or
false
to indicate whether you want to continue collecting the flow
👀 1
I’m not sure why
collectWhile
(which exists) isn’t a public function 🤔 but
transformWhile { … }.collect()
achieves the same thing
🧐 1
m

mgrazianodecastro

02/14/2023, 1:31 PM
from what you suggested, looking at the docs, we have
takeWhile
👍 1
w

Wout Werkman

02/14/2023, 1:32 PM
what type* does
longRunningTask
return?
m

mgrazianodecastro

02/14/2023, 1:32 PM
just a String
s

Sam

02/14/2023, 1:34 PM
A String, or a
Flow<String>
?
🙏 1
m

mgrazianodecastro

02/14/2023, 1:34 PM
Flow<String>
👍 1
s

Sam

02/14/2023, 1:34 PM
Just checking 😄
m

mgrazianodecastro

02/14/2023, 1:34 PM
sometimes I'm lazy to explain things, my bad
since the condition in my case is to check whether a new message arrived in a channel,
channel.isEmpty()
, the
takeWhile{ channel.isEmpty }
seems to be working
👍 1
🐕 2
2 Views