Kotlin Flow 与 Android LiveData

Kotlin Flow vs Android LiveData

我对 Kotlin Flow

有一些疑问
  1. 我可以从多个片段中观察到 LiveData。我可以用 Flow 做这个吗?如果是那么怎么办?
  2. 我们可以使用 map& switchMap 从单个 LiveData 中得到多个 LiveData。有什么方法可以从单一来源Flow获得多个Flow吗?
  3. 使用 MutableLiveData 我可以使用变量引用从任何地方更新数据。有什么方法可以对 Flow 做同样的事情吗?

我有一个这样的用例:我将使用 callbackFlow{...} 观察 SharedPreferences,这将给我一个单一的源流。从该流中,我想为每个键值对创建多个流。

这些问题听起来可能很愚蠢。我是 Rx 和 Flow 世界的新手。

I can observe LiveData from multiple Fragments. Can I do this with Flow? If yes then how?

是的。您可以使用 emitcollect 来完成此操作。认为 emit 类似于实时数据 postValuecollect 类似于 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)
    }
}

ViewModel

fun getWeatherForecast(): Flow<String> {
    return forecastRepository.getWeatherForecastEveryTwoSeconds()
}

片段

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
            }
    }
}

We can have multiple LiveData from a single LiveData using map& switchMap. Is there any way to have multiple Flow from a single source Flow?

流量非常好用。您可以在流程中创建流程。假设您想在每个天气预报数据上附加度数符号。

ViewModel

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) 
                }
    }
}

然后和#1一样在Fragment中收集数据。这里发生的是视图模型正在从存储库收集数据,片段正在从视图模型收集数据。

Using MutableLiveData I can update data from anywhere using the variable reference. Is there any way to do the same with Flow?

你不能在流程之外发出价值。 flow里面的代码块只有在有收集器的时候才会执行。但是您可以使用 LiveData 的 asLiveData 扩展将流转换为实时数据。

ViewModel

fun getWeatherForecast(): LiveData<String> {
    return forecastRepository
    .getWeatherForecastEveryTwoSeconds()
    .asLiveData() // Convert flow to live data
}

在你的情况下你可以这样做

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
}

编辑

感谢@mark 的评论。在视图模型中为 getWeatherForecast 函数创建一个新流实际上是不必要的。它可以重写为

fun getWeatherForecast(): Flow<String> {
        return forecastRepository
                .getWeatherForecastEveryTwoSeconds(spendingDetailsRequest)
                    .map {
                        it + " °C"
                    }
    }

新的 androidx.lifecycle ktx 包中有一个新的 Flow.asLiveData() 扩展功能。您可以在我的文章中了解更多信息: https://www.netguru.com/codestories/android-coroutines-%EF%B8%8Fin-2020

在 3 层架构中:数据域表示,Flow 应发生在数据层(数据库、网络、缓存...),然后如 Samuel Urbanowicz 所述,您可以将 Flow 映射到LiveData.

总的来说,Flow 几乎就是 RxJava 的 Observable(或 Flowable)。不要将它与 LiveData 混淆。

更多信息:https://medium.com/@elizarov/cold-flows-hot-channels-d74769805f9

只是想在这里补充 Fatih 的回答,因为它已经有一段时间了。

I can observe LiveData from multiple Fragments. Can I do this with Flow? If yes then how?

是的。但是您应该这样做的方式有所改变。您应该使用 repeatOnLifecycle 来更安全地从 Flows post 到 UI。它是新的,文档很少,但它看起来像这样:

lifecycleScope.launch {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
        viewModel.getWeatherForecast().collect {
           // Safely update the UI
        }
    }
}

这可确保天气预报仅在 UI 显示时更新,不会浪费资源。是的,您可以同时对来自同一流的多个片段执行此操作。

We can have multiple LiveData from a single LiveData using map& switchMap. Is there any way to have multiple Flow from a single source Flow?

这是显而易见的。流有大量的运算符 like map and switchMap

Using MutableLiveData I can update data from anywhere using the variable reference. Is there any way to do the same with Flow?

是的。我们现在有 MutableStateFlow,它非常接近并且比 MutableLiveData 更强大。

val textFlow = MutableStateFlow("Hello")

someButton.onPress {
    textFlow.value = "World"
}

lifecycleScope.launch {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
        textFlow.collect {
           someTextView.text = it
        }
    }
}

上面的SharedPreferences代码可以稍微修改一下:

private fun getSharedPrefFlow() = callbackFlow {
    val sharedPref = context.getSharedPreferences("SHARED_PREF_NAME", MODE_PRIVATE)
    sharedPref?.all?.forEach {
        trySend(it) // offer is deprecated
    }
}

init {
    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            getSharedPrefFlow()
                .filter { it.key == BIRTHDAY_KEY }
                .collect {
                    birthdayTextView.text = it.value
                }
        }
    }
}