When I am collecting how do I cancel a flow, I got...
# coroutines
l
When I am collecting how do I cancel a flow, I gotta throw an error?
d
Throw a cancellation exception. Or call cancel if it's available in your context.
e
Cancel the collector
l
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
You're not collecting the flow.
r
cancel()
l
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