xxfast
07/06/2021, 11:53 PMFlow<T>.flowOn(dispatcher)
equivalent that that affects downstream? so that i can do
results
.map { result -> format(result) }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
.collectOn(Dispatchers.Main)
.collect { presenter.display(it) }
instead of
withContext(Dispatchers.Main) {
results
.map { result -> format(result) }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
.collect {
presenter.display(it)
}
}
streetsofboston
07/07/2021, 12:02 AMwithContext(Dispatchers.Main) {
results
.map { result -> format(result) }
.flowon(<http://Dispatchers.IO|Dispatchers.IO>)
.collect { ... }
}
since collect
is a suspending functionursus
07/07/2021, 12:15 AMNick Allen
07/07/2021, 12:19 AMflowWith
used to exist as part of the Flow preview library but was removed because it didn't play well with other coroutine/flow semantics.Nick Allen
07/07/2021, 12:23 AMresults
.map { result -> format(result) }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
.onEach { presenter.display(it) }
.flowOn(Dispatchers.Main)
.collect()
xxfast
07/07/2021, 2:55 AMxxfast
07/07/2021, 2:55 AM.onEach
instead of .collect
?ephemient
07/07/2021, 3:58 AM.launchIn(scope)
myungpyo.shim
07/07/2021, 8:42 AMprivate fun sample1() {
membershipUiData
.map { /* IO Thread */ }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
.onEach { data ->
/* Main thread */
}
.launchIn(viewModelScope)
}
private suspend fun sample2() = viewModelScope.launch {
membershipUiData
.map { /* IO Thread */ }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
.collect { data ->
/* Main thread */
}
}
(viewModelScope is composed of Dispatchers.main.immediate + SupervisorJob. So, if you are not using ViewModel, you can define appropriate context by yourself.)ephemient
07/07/2021, 9:10 AMviewLifecycleOwner.repeatOnLifecycle(STARTED) {
flow.collect { ... }
}
instead, https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda