Concurrency with Kotlin Flow

Jun 5 2024 · Kotlin 1.9.20, Android 14, Android Studio Iguana

Lesson 04: Advanced Flow Management

Hot Streams Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll see an example of a hot flow. Start Android Studio and open the 04-advanced-flow-management/Starter folder. As mentioned in previous lessons, make sure to use the starter project and not the project you had from the previous lessons, because we have added some helper functions.

fun moviesByCategories(): Flow<Map<String, List<Movie>>> = movieDatabase.getMoviesByCategoryFlow()

suspend fun updateMoviesByCategories(category: String) {
  val moviesForCategory = moviesByCategoryDummyData[category]
  val shuffledMovies = moviesForCategory!!.shuffled()
  movieDatabase.updateMoviesByCategory(category = category, movies = shuffledMovies)
}
private fun fetchMoviesByCategories() {
  viewModelScope.launch {
    movieRepository.moviesByCategories() // HERE
      .transform { moviesByCategories -> 
        //...
      }
      .collect {
        _moviesByCategories.emit(it)
      }
  }   
}
fun refreshCategory(category: String) {
  viewModelScope.launch {
    movieRepository.updateMoviesByCategories(category)
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Cold & Hot Streams in Kotlin Flow Next: Handling Errors in Kotlin Flow