hi! how can i create a flow where the emitting par...
# coroutines
b
hi! how can i create a flow where the emitting part runs in a different thread / coroutine context ? assume i have an class which has a method getRemoteService() : Flow<Result>. when calling that method i want the method to immediately start consuming the web service so when i call flow.collect{} it is either already finished or still in the process of fetching. somehow with the flow builders i haven't found a way to do that. should i use in that case a channel directly?
g
z
You could use a regular flow to actually perform the request, but then in your method use the
shareIn
operator to subscribe eagerly.
b
hmm the docs of shareIn says its for multiple consumers. also, somehow code where i use shareIn wont compile .. weird. idea thinks the method is there but the compiler says "Kotlin: Unresolved reference: shareIn" i wrote a mini example here https://github.com/bitkid/ksparql/blob/main/src/test/kotlin/com/bitkid/ksparql/TestClient.kt when the flow is collected it starts reading the bytereadchannel .. not sure how i can start reading directly
z
It’s often used for multiple consumers, but you can also use it for this.
b
it seems that shareIn does not close the flow even if the underlying flow is done