Skip to content

Instantly share code, notes, and snippets.

View JoseAlcerreca's full-sized avatar

Jose Alcérreca JoseAlcerreca

View GitHub Profile
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeatherFlow: Flow<String> =
dataSource.fetchWeatherFlow()
.map { ... }
.filter { ... }
.dropWhile { ... }
.combine { ... }
.flowOn(Dispatchers.IO)
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeatherFlow: LiveData<String> =
dataSource.fetchWeatherFlow()
.map { heavyTransformation(it) }
.asLiveData()
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeatherLiveData: LiveData<String> =
dataSource.fetchWeather().switchMap {
liveData { emit(heavyTransformation(it)) }
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeatherFlow: LiveData<String> =
dataSource.fetchWeatherFlow()
.onStart { emit(LOADING_STRING) }
.asLiveData()
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// Don't use this
val currentWeatherFlow: LiveData<String> = liveData {
emit(LOADING_STRING)
emitSource(
dataSource.fetchWeatherFlow().asLiveData()
)
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeather: LiveData<String> = liveData {
emit(LOADING_STRING)
emitSource(dataSource.fetchWeather())
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeatherFlow: LiveData<String> = dataSource.fetchWeatherFlow().asLiveData()
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// Don't use this
val currentWeatherFlow: LiveData<String> = liveData {
dataSource.fetchWeatherFlow().collect {
emit(it)
}
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val currentWeather: LiveData<String> = dataSource.fetchWeather()
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class MyViewModel {
val result = liveData {
emit(repository.fetchData())
}
}