```flow.flatMapLatest { flowValue -> state...
# coroutines
b
Copy code
flow.flatMapLatest { flowValue -> 
    stateFlowDependingOnFlowValue(flow1Value,scope) // internally does something like `.stateIn(scope)`
}
Given this code, every time
flatMapLatest
is called, a new coroutine is spawned in the scope. Is there a way to use a scope, which is only active within
flatMapLatest
, so old calculations would be cancelled?
b
smth like transformLatest + coroutineScope should work:
Copy code
flow.transformLatest { flowValue ->
   coroutineScope {
      emitAll(stateFlowDependingOnFLowValue(flowValue, this))
   }
}
b
Wouldn't
coroutineScope
prevent
transformLatest
to complete, because of
stateIn
?
b
of course it will, but it will properly handle cancellation (flatMapLatest is doing exactly what I wrote but without coroutineScope since it doesn't need an access to it)
👍 1
if you stick to flatMapLatest, I'm afraid you will have to wrap it to one more
flow {}
+
coroutineScope { }
:
Copy code
flow.flatMapLatest { flowValue -> 
  flow {
     coroutineScope { 
        emitAll(stateFlowDependingOnFlowValue(flow1Value,scope))
     }
  }
}
b
Thank you very much, the solution with
transformLatest
seems to be perfect :)
👍 1