# Strategies for automatically refreshing data on Android using Kotlin Flow

DevFeed: [Strategies for automatically refreshing data on Android using Kotlin Flow](<https://devfeed.tech/articles/strategies-for-automatically-refreshing-data-on-android-using-kotlin-flow-25884.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/strategies-for-automatically-refreshing-data-on-android-using-kotlin-flow-cd23ba7cfbe0?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2023-10-06T17:27:03Z

Content type: tutorial

Language: en

Sources: [Stories by Christophe Beyls on Medium](<https://devfeed.tech/sources/stories-by-christophe-beyls-on-medium.md>)

Topics: [kotlin-flow](<https://devfeed.tech/topics/kotlin-flow.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android](<https://devfeed.tech/topics/android.md>), [RxJava](<https://devfeed.tech/topics/rxjava.md>), [ui](<https://devfeed.tech/topics/ui.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [api](<https://devfeed.tech/tags/api.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-flow](<https://devfeed.tech/tags/kotlin-flow.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [sharedflow](<https://devfeed.tech/tags/sharedflow.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This tutorial explains how to periodically refresh data in Android user interfaces with Kotlin Flow. It compares sequential map() processing with concurrent, cancellation-aware mapLatest() processing and discusses lifecycle-aware refreshing and caching considerations.

## Source excerpt

Making timers lifecycle-aware This is the third part of a series of articles discussing the usage of Kotlin Flow to efficiently load data in Android applications. It's a direct follow-up to part 2: "Smarter Shared Kotlin Flows", as it reuses the same concepts to cover another use case: automatic periodic refresh of the user interface. Simple periodic refresh When it's not possible to determine precisely when a data set displayed by the UI has changed, or when it changes too frequently, a common strategy is to reload the data periodically at a fixed interval while the screen is visible. One of the simplest ways to achieve this is to create a Flow from an infinite loop calling delay() between emissions: fun tickerFlow(period: Duration): Flow<Unit> = flow { while (true) { emit(Unit) // Tick delay(period) } } This is equivalent to the Observable.interval() operator in RxJava with a fixed emitted value (Unit) and an initial delay of 0. Then, transform this Flow using the map() or mapLatest() operator to perform the loading action on each "tick" of the timer and return the result: tickerFlow(REFRESH_INTERVAL) .map { repository.loadSomeData() } Note the subtle difference in behavior between the two operators: With map() the entire Flow will be executed in sequence within a single coroutine, meaning delay() will only start running after the loading operation completes. As a result, each loading operation will be delayed by the amount of time it took to perform the previous loading operation, plus the fixed interval. With mapLatest() the main coroutine will collect the upstream values of tickerFlow() while a child coroutine will be created to perform the loading operation concurrently and collect the result without suspending the main coroutine. This means that delay() will start running immediately after the previous tick and each loading operation will start precisely according to schedule. This also means that the interval must be longer than the typical loading time beca