https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Luis Munoz

04/09/2020, 7:08 PM
When I am collecting how do I cancel a flow, I gotta throw an error?
d

Dominaezzz

04/09/2020, 7:25 PM
Throw a cancellation exception. Or call cancel if it's available in your context.
e

Erik

04/09/2020, 9:10 PM
Cancel the collector
l

Luis Munoz

04/09/2020, 11:38 PM
Copy code
merge(sampling(), samplingCheckFlow()).flowOn(context)
private fun samplingCheckFlow() : Flow<String> {
  return flow<String> {
    <http://Log.info|Log.info>().log("cehcking flow!")
    while(isSampling.get()) { delay(1000L) }
    <http://Log.info|Log.info>().log("[kafka sampling] - throw cancellation")
    throw CancellationException("not sampling")
  }
}
i thow and still doesn't cancel
d

Dominaezzz

04/10/2020, 1:12 AM
You're not collecting the flow.
r

rkeazor

04/10/2020, 3:06 AM
cancel()
l

Luis Munoz

04/10/2020, 5:11 PM
thanks guys, I didn't ask the question correctly by not showing details but my mistake was I was doing .first() instead of .toList().first() in my test case
3 Views