Kotlin Flow에 대한 질문이 있습니다
- 여러 조각에서 LiveData를 볼 수 있습니다. Flow로이 작업을 수행 할 수 있습니까? 그렇다면 어떻게?
 map&를 사용하여 단일 LiveData에서 여러 개의 LiveData를 가질 수 있습니다switchMap. 단일 소스 플로우에서 여러 플로우를 가질 수있는 방법이 있습니까?- 사용 
MutableLiveData난 아무데도 변수 참조를 사용하여 데이터를 업데이트 할 수 있습니다. Flow와 동일한 작업을 수행 할 수있는 방법이 있습니까? 
나는 같은 사용 사례가 : 나는이 관찰됩니다 SharedPreferences사용 callbackFlow{...}하는 나에게 단일 소스 흐름을 제공 할 것입니다. 해당 흐름에서 각 키-값 쌍에 대해 여러 개의 흐름을 만들고 싶습니다.
이것은 어리석은 질문으로 들릴 수 있습니다. 나는 Rx와 Flow 세계에 처음이다.
답변
여러 조각에서 LiveData를 볼 수 있습니다. Flow로이 작업을 수행 할 수 있습니까? 그렇다면 어떻게?
예. emit및 로이 작업을 수행 할 수 있습니다 collect. 생각 emit은 실제 데이터 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)
    }
}
뷰 모델
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.getWeatherForecastEveryTwoSeconds().collect {
                    // Use the weather forecast data
                    // This will be called 3 times since we have 3 
                    // weather forecast data
            }
    }
}
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)
                }
    }
}
그런 다음 # 1과 같은 조각으로 데이터를 수집하십시오. 여기서 발생하는 일은 뷰 모델이 저장소에서 데이터를 수집하고 조각이 뷰 모델에서 데이터를 수집하는 것입니다.
MutableLiveData 사용 변수 참조를 사용하여 어디서나 데이터를 업데이트 할 수 있습니다. Flow와 동일한 작업을 수행 할 수있는 방법이 있습니까?
흐름 외부에서 값을 방출 할 수 없습니다. 플로우 내부의 코드 블록은 콜렉터가있는 경우에만 실행됩니다. 그러나 LiveData에서 asLiveData 확장을 사용하여 흐름을 라이브 데이터로 변환 할 수 있습니다.
뷰 모델
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"
                    }
    }
답변
Flow.asLiveData()새로운 androidx.lifecyclektx 패키지 에는 새로운 확장 기능이 있습니다. 내 기사에서 더 많은 것을 배울 수 있습니다 :
 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