Not sure what context does an inner flow operate on

Viewed 66

I have a question about the context in which an inner flow produces

Consider the following code.

      withContext(Dispatchers.Main)
      {
        val flowA = flowOf(1,2,3,4)
        val flowB = flowOf("a","b","c")
        flowA.flatMapConcat { num->
          val bResponse = flowB.first() //would this operate on Dispatchers.Main OR IO
          flowOf(bResponse)
        }.flowOn(Dispatchers.IO)
      }
    }

I have a nested flow and I am unsure on what context would the flowB.first() terminal operator operate on ?

1 Answers

As you can see in the documentation,

flowOn changes the context where this flow is executed to the given context.

Since val bResponse = flowB.first() is executed inside flowA, its context is changed to Dispatchers.IO by the flowOn operator.

Related