Had a coworker pose this code snippet for me and a...
# coroutines
b
Had a coworker pose this code snippet for me and ask me what would break if anything. I'd like to see what you all think as well:
Copy code
override fun getSomeFlow(): Flow<String> =
        localDataFlow
            .onStart {
                // Fire-and-forget, but still tied to the collector's lifecycle.
                CoroutineScope(currentCoroutineContext()).launch {
                    triggerSomePrefetchCaching()
                }
            }
            .map { data ->
                data.toSomeString()
            }
My initial reaction was: "Thanks, I hate it". Looking at it though, the issues I started to wonder were: • How would this cancellations from this impact the flow? • Would exceptions be passes along down flow? • Would
onStart
delay completion until all coroutines launched this way have completed (similar to
coroutineScope {}
being called)?
j
The worst part that immediately freaks me out is the new
CoroutineScope
for no apparent reason
☝🏼 2
☝️ 1
b
Yea, that was a "wtf are you doing, dude" reaction from me.
😄 1
c
The theory kinda makes sense, but I definitely think there are safer and clearer ways to do it. For example, using the
flow { }
builder and launching the prefetch Coroutine in that block.
☝🏼 1