简体   繁体   English

使用 Flow 进行 LiveData 转换

[英]LiveData Transformations with Flow

Has I read more about Flow I'm starting to think that you can replace MutableLiveData switchMap pattern with something similar with flow.我是否阅读了有关 Flow 的更多信息?我开始认为您可以用与 flow 类似的东西替换 MutableLiveData switchMap 模式。

You can replace LiveData observation with flow by implementing something like this but I'm unable to imagine how to replace the switchMap part.您可以通过实现类似东西来用流替换 LiveData 观察,但我无法想象如何替换 switchMap 部分。

By switchMap pattern I mean this:通过 switchMap 模式,我的意思是:

val mutableLiveData: MutableLiveData<SomeClass> = MutableLiveData()
val liveData: LiveData<SomeOtherClass> = mutableLiveData
    .switchMap {
       //call to repo
    }

Is there anything equivalent or am I missing out something completely?有什么等价的还是我完全错过了什么? Or even thinking about flow completely the wrong way.甚至以完全错误的方式思考流程。

The equivalent is Flow.flatMapLatest((T) -> Flow<R>) .等效的是Flow.flatMapLatest((T) -> Flow<R>) They actually removed the.他们实际上删除了。 Flow.switchMap function in favour of a few new mapLatest functions. Flow.switchMap function 支持一些新的mapLatest函数。

However this has been marked as experimental for now so use it with caution.但是,这目前已被标记为实验性的,因此请谨慎使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM