zoh*_*131 60 android kotlin android-livedata kotlin-coroutines kotlin-flow
我有一些关于Kotlin Flow 的问题
LiveData从多个 Fragment观察。我可以这样做Flow吗?如果是,那么如何?LiveData从单个中获得多个。有没有办法从一个来源获得多个?LiveDatamapswitchMapFlowFlowMutableLiveDataI 可以使用变量引用从任何地方更新数据。有什么办法可以做同样的事情Flow吗?我有一个用例,例如:我将观察一个SharedPreferencesusing callbackFlow{...},它会给我一个单一的源 Flow。从那个 Flow 中,我想为每个键值对创建多个 Flow。
这些可能听起来很愚蠢的问题。我是 Rx 和 Flow 世界的新手。
Fat*_*tih 56
我可以从多个 Fragment 观察 LiveData。我可以用 Flow 做到这一点吗?如果是,那么如何?
是的。你可以用emit和来做到这一点collect。Thinkemit类似于实时数据postValue,collect类似于observe. 让我们举一个例子。
存储库
// I just faked the weather forecast
val weatherForecast = listOf("10", "12", "9")
// This function returns flow of forecast data
// Whenever the data is fetched, it is emitted so that
// collector can collect (if there is any)
fun getWeatherForecastEveryTwoSeconds(): Flow<String> = flow {
for (i in weatherForecast) {
delay(2000)
emit(i)
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型
fun getWeatherForecast(): Flow<String> {
return forecastRepository.getWeatherForecastEveryTwoSeconds()
}
Run Code Online (Sandbox Code Playgroud)
分段
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// Collect is suspend function. So you have to call it from a
// coroutine scope. You can create a new coroutine or just use
// lifecycleScope
// https://developer.android.com/topic/libraries/architecture/coroutines
lifecycleScope.launch {
viewModel.getWeatherForecast().collect {
// Use the weather forecast data
// This will be called 3 times since we have 3
// weather forecast data
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以使用 map 和 switchMap 从单个 LiveData 获得多个 LiveData。有没有办法从单个源流中获得多个流?
流量非常方便。您可以在流中创建流。假设您要为每个天气预报数据附加度数符号。
视图模型
fun getWeatherForecast(): Flow<String> {
return flow {
forecastRepository
.getWeatherForecastEveryTwoSeconds(spendingDetailsRequest)
.map {
it + " °C"
}
.collect {
// This will send "10 °C", "12 °C" and "9 °C" respectively
emit(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在与 #1 相同的 Fragment 中收集数据。这里发生的是视图模型从存储库收集数据,片段从视图模型收集数据。
使用 MutableLiveData 我可以使用变量引用从任何地方更新数据。有没有办法对 Flow 做同样的事情?
您不能在流程之外发出价值。flow里面的代码块只有在有收集器的时候才会执行。但是您可以使用 LiveData 中的 asLiveData 扩展将流转换为实时数据。
视图模型
fun getWeatherForecast(): LiveData<String> {
return forecastRepository
.getWeatherForecastEveryTwoSeconds()
.asLiveData() // Convert flow to live data
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下,你可以这样做
private fun getSharedPrefFlow() = callbackFlow {
val sharedPref = context?.getSharedPreferences("SHARED_PREF_NAME", MODE_PRIVATE)
sharedPref?.all?.forEach {
offer(it)
}
}
getSharedPrefFlow().collect {
val key = it.key
val value = it.value
}
Run Code Online (Sandbox Code Playgroud)
编辑
感谢@mark 的评论。在视图模型中为getWeatherForecast函数创建一个新流实际上是不必要的。可以改写为
fun getWeatherForecast(): Flow<String> {
return forecastRepository
.getWeatherForecastEveryTwoSeconds(spendingDetailsRequest)
.map {
it + " °C"
}
}
Run Code Online (Sandbox Code Playgroud)
Flow.asLiveData()新的androidx.lifecyclektx 包中有一个新的扩展功能。您可以在我的文章中了解更多信息:https :
//www.netguru.com/codestories/android-coroutines-%EF%B8%8Fin-2020
在 3 层架构中:数据域表示,流应该发生在数据层(数据库、网络、缓存……),然后正如Samuel Urbanowicz提到的,您可以将 Flow 映射到 LiveData。
一般来说,Flow 几乎就是 RxJava 的 Observable(或 Flowable)。不要将它与 LiveData 混淆。
更多信息:https : //medium.com/@elizarov/cold-flows-hot-channels-d74769805f9
| 归档时间: |
|
| 查看次数: |
22568 次 |
| 最近记录: |