Kotlin Flows in practice

Android Developers · 198K views · 2021 · 21:06

Open on YouTube

Software practice, open source & careersFunctional programmingtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining how to collect Flows safely in Android, especially whether repeatOnLifecycle is needed, and as a general introduction to how Flows and coroutines fit together.

Recommended by 2 people across 3 different months, 2021 to 2023.

What it is

An Android Dev Summit 2021 session by Manuel Vicente Vivo and Jose Alcérreca on using Kotlin Flow in Android apps. It covers the Flow APIs across app layers, the lifecycle pitfalls of collecting flows, and the newer lifecycle-aware APIs such as repeatOnLifecycle.

What people said

Recommends cactustictacs · 2021-11 · Stack Overflow · on “I want to use Kotlin Flow to update my UI when SharedPreferences have changed”

Have a look at the Kotlin docs for it: [LINK] The basic idea is you create a Flow , and it can produce values over time. You run collect() on that in a coroutine, which allows you to asynchronously handle those updates as they come in. Generally that flow does a bunch of work internally, and just emits values as it produces them. You could use this within a class as a kind of worker task, but a lot of the time you'd expose flows as a data source, for other components to observe. So you'll see, for example, repositories that return a Flow when you try to get a thing - it's basically "ok we don't have that yet, but it'll come through here". I'm not an expert on them, and I know there are some

Recommends Constructor · 2022-10 · Stack Overflow · on “Collecting Flows in ViewModel. Is repeatOnLifeCycle needed?”

TLDR: In ViewModel, use stateIn : someFlow.stateIn( scope = viewModelScope, initialValue = , // set initial value here started = SharingStarted.WhileSubscribed(5000) ) This is likely the best method if configuration change needs to be taken care of. The same, if you are willing to add an unnecessary LiveData into the mix, can actually be achieved with arguably less code using someFlow.asLiveData() which also defaults to timeoutInMs = 5000 . (See additional note that bottom) OR In Activity onCreate /Fragment onCreateView , use: repeatOnLifecycle. (All main conclusions in big font below) I was able to clarify many concepts after going through the full video explanation (link to relevant part o

Recommends cactustictacs · 2022-11 · Stack Overflow · on “How should I go about implementing MVVM architecture pattern in my project?”

@Balizok no worries, glad it helped! It's a big topic and there's a lot to get your head around, and often a lot of changes you have to make if you've already designed things another way (ask me how I know) but it makes a lot of sense when you understand the bigger picture. This might be handy too: [this video] it gets into some of the technicalities of coroutines and Flows, so don't worry too much if that's a bit confusing right now, but I think it does a nice job of giving you a sense of what's going on and why it's designed that way. Then you can use the parts you like!

Cites ThanosFisherman · 2021-11 · Stack Overflow · on “Collecting Flows in ViewModel. Is repeatOnLifeCycle needed?”

Up until now I used to collect my flows either in activity/fragment or in ViewModel like so Activity/Fragment lifecycleScope.launch { myViewModel.readTokenCredentials().collect { data -> /* do something */ } } ViewModel viewModelScope.launch { prefsRepo.readTokenCredentials().collect { data -> /* do something */ } } Now Google devs tell [this video] us that this is not a safe way to collect flows because it could lead to memory leaks. Instead they recommend wrapping the collection in lifecycle.repeatOnLifecycle for flow collection in Activities/Fragments. lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { myViewModel.readTokenCredentials().collect { data -> /* do somet

Cites cactustictacs · 2023-02 · Stack Overflow · on “When I should use Flow, and when should I use Corountines?”

To add to what Tenfour04 is saying, coroutines are about concurrency - they allow you to do multiple tasks "at the same time" instead of having a single task where each step can only take place when the step before has finished. That means you can start tasks that go off and take a while to produce a result, while the current task continues instead of having to wait for it (which is called blocking ). So when you're using a coroutine, you're able to do stuff that "takes a while" . There are two main patterns for this: things that return a single result, or no result at all things that return multiple results over time The first includes coroutines created with launch (no result) or async (re