# Real-time lifecycle-aware updates in Jetpack Compose

DevFeed: [Real-time lifecycle-aware updates in Jetpack Compose](<https://devfeed.tech/articles/real-time-lifecycle-aware-updates-in-jetpack-compose-25898.md>)

Original publisher: [Read original article](<https://proandroiddev.com/real-time-lifecycle-aware-updates-in-jetpack-compose-be2e80e613c2?source=rss-56174fa84bcc------2>)

Author: Denys Rudenko

Published: 2021-04-18T21:20:35Z

Content type: tutorial

Language: en

Sources: [Stories by Denis Rudenko on Medium](<https://devfeed.tech/sources/stories-by-denis-rudenko-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [real-time](<https://devfeed.tech/topics/real-time.md>), [ui](<https://devfeed.tech/topics/ui.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [compose](<https://devfeed.tech/tags/compose.md>), [flow](<https://devfeed.tech/tags/flow.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [real-time](<https://devfeed.tech/tags/real-time.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

A tutorial on implementing real-time, lifecycle-aware updates in Jetpack Compose using Kotlin flows and ViewModel scope. It demonstrates podcast download progress and currency updates for visible items, including flow creation, observation, cancellation, and UI recomposition.

## Source excerpt

This is a journey into real-time updates, flows creation, cancelation, and proper lifecycle scoping. We'll be exploring all of this using two examples: 1. Imitating podcast download progress. (on the right) 2. Imitating currency updates for visible items. (on the left) https://medium.com/media/a7a27205a34388e3956fbca89b1b9b2d/hrefPodcast downloadsStep 1: Creating podcasts ViewModelhttps://medium.com/media/44ad93376981982842b9bb4096f93d4b/href We're using getPodcasts() to populate list of podcasts, which we then put into our _podcasts, which is a MutableStateFlow. We'll be observing podcasts from our UI layer to build a LazyColumn of cards. downloadQueue map will be used to store active downloads. Step 2: Creating podcasts UIhttps://medium.com/media/70c8b3ab1157a3c847272f361797a6c1/hrefPodcastsScreen Observes viewModel.podcasts containing with the help of .collectAsState(). This means that list of cards will be diffed & recomposed each time the value of viewModel.podcasts changes. Whenever user taps on the download icon, we invoke the onDownloadPodcastClicked(), which we'll declare in the next step in the viewModel. PodcastCard Composable that contains podcast title & download status. Step 3: Imitating the download & updating the UI Let's add these 3 functions into the PodcastsViewModel. https://medium.com/media/e0d4da5c83948894c3960db82a5ff715/hrefonDownloadPodcastClicked() Checks whether downloadQueue already has specified podcastId inside. If it does - we do nothing, since this means that we already have an active download for that podcast. Creates the download flow using provideDownloadFlow():Flow<Int> to imitate the download & puts it in our downloadQueue map using podcastId as a key. Starts observation of the download progress by calling observeDownload(). provideDownloadFlow() Creates a flow which emits random numbers in range between 10 & 25 representing the download percentage in 0.5 second intervals. Whenever progress is 100 -- it removes itself from the dow