What would be the best way to load data, before fl...
# coroutines
p
What would be the best way to load data, before flow will be collected? Does following make sense?
Copy code
val flow = MutableStateFlow<String?>(null)

fun get(): Flow<String> {
    // If not loaded yet
    if (flow.value == null) {
        val init = flowOf {
            load()
            emit(flow.value)
        }
        return combine(init, flow.asSharedFlow())
    } else {
        flow.asSharedFlow()
    }
}

suspend fun load() = TODO()
Or maybe something like following?
Copy code
flow.asSharedFlow().onStart { load() }
m
onStart
is a good option to process data before the flow is collected.
👍 2