使用 Flow、StateFlow、SharedFlow 发布价值时避免重复启动
我正在从 LiveData 迁移到协程流,特别是 StateFlow 和 SharedFlow。不幸的是,发出值应该在 CoroutineScope 上运行,因此在 ViewModel 中使用它时,您会遇到丑陋的重复代码 viewModelScope.launch
。是否有一种最佳的方式来释放价值?
class MainSharedViewModel : BaseViewModel() {
private val mainActivityState = MutableSharedFlow<MainActivityState>()
fun getMainActivityState(): SharedFlow<MainActivityState> = mainActivityState
fun setTitle(title: String){
viewModelScope.launch {
mainActivityState.emit(ToolbarTitleState(title))
}
}
fun filterData(assetName: String){
viewModelScope.launch {
mainActivityState.emit(AssetFilterState(assetName))
}
}
fun limitData(limit: Int){
viewModelScope.launch {
mainActivityState.emit(AssetLimitState(limit))
}
}
}
I am migrating from LiveData
to Coroutine Flows specifically StateFlow
and SharedFlow
. Unfortunately emitting values should run on a CoroutineScope thus you have this ugly repetitive code viewModelScope.launch
when using it inside a ViewModel. Is there an optimal way of emitting values from this?
class MainSharedViewModel : BaseViewModel() {
private val mainActivityState = MutableSharedFlow<MainActivityState>()
fun getMainActivityState(): SharedFlow<MainActivityState> = mainActivityState
fun setTitle(title: String){
viewModelScope.launch {
mainActivityState.emit(ToolbarTitleState(title))
}
}
fun filterData(assetName: String){
viewModelScope.launch {
mainActivityState.emit(AssetFilterState(assetName))
}
}
fun limitData(limit: Int){
viewModelScope.launch {
mainActivityState.emit(AssetLimitState(limit))
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
tryEmit()
而不是emit()
。tryEmit()
是非挂起的。它“尝试”的原因是,如果流的缓冲区当前已满并设置为 SUSPEND,则它不会发出,而不是在满时丢弃值。请注意,您当前没有缓冲区,因为您将
replay
保留为 0。您应该将重播保持至少1
,这样当配置发生更改时就不会丢失值您的活动/片段。示例:
或者,您可以使用 MutableStateFlow,它的重播始终为 1,并且可以使用
value =
设置其值,就像 LiveData 一样。Use
tryEmit()
instead ofemit()
.tryEmit()
is non-suspending. The reason it's "try" is that it won't emit if the flow's buffer is currently full and set to SUSPEND instead of dropping values when full.Note, you have no buffer currently because you left
replay
as 0. You should keep a replay of at least1
so values aren't missed when there is a configuration change on your Activity/Fragment.Example:
Alternatively, you can use MutableStateFlow, which always has a replay of 1 and can have its value set by using
value =
, just like a LiveData.