Eslam Hussein
10/09/2023, 6:42 AMclass A(private val anyState:StateFlow<Int>){
fun doSomething(state:StateFlow<String>){
......
}
}
Jakub Gwóźdź
10/09/2023, 9:16 AMfun StateFlow<String>.doSomethingUsing(foo: Foo) { ... }
(after all, most of Flow API is exactly that - extension functions that do something on this
flow and returns some result)Eslam Hussein
10/09/2023, 10:33 AMErfannj En
10/09/2023, 11:09 AMCasey Brooks
10/09/2023, 6:33 PMFlow
could be problematic, since it will restart the entire flow for each Collector, but intuitively one might think the Flow is run only once and its values handled by multiple Collectors. Either pass in a SharedFlow to explicitly indicate a single Flow with multiple collectors, or pass in an object which can be used to create a new Flow, to make it explicit that each Collector is restarting the entire pipeline. For example:
public interface FlowCreator {
fun newFlow(): Flow<String>
}
class A {
suspend fun doSomething(flowCreator: FlowCreator) {
flowCreator.newFlow().collect { }
}
}
viewModelScope
), but the operator itself it doesn’t really care exactly where it’s used.
Passing it into a function/class implies that the Flow is being collected in there. The lifecycle of the Flow, then, is related to that particular class/function. For example, passing a Flow into a ViewModel implies that the Flow will be collected in that ViewModel’s viewModelScope
.