-1

I have a BroadcastChannel as a buffer. I send values at one scope and would like to observe them using flow at another.

My code is:

val channel = BroadcastChannel<Int>(1)

val observable = channel.asFlow()

someScope.launch {
   channel.send(42)
}

otherScope.launch {
  observable.collect {
    print(it)
  }
}

Some information:

  1. otherScope lives longer then someScope
  2. When collect at the same scope I received values
Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
  • You should probably use `SharedFlow` instead. It's going to replace `BroadcastChannel` in the future: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/ – marstran Apr 27 '21 at 11:43
  • Anyway, what exactly is the problem with your code? What did you expect, and what actually happens? – marstran Apr 27 '21 at 11:46
  • @marstran I expect to receive value from channel in another scope – Alex Klimashevsky Apr 27 '21 at 11:48

1 Answers1

0

The issue was with collect operator that block other collections. Have replaced with onEach and launchIn and everything works as expected

Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58