Clean Flow Transformations with Combine, Zip & Merge - Kotlin Flows

preview_player
Показать описание
Kotlin Flows are an amazing way of leveraging coroutines for reactive programming. In this video, you'll learn about the differences between combine, zip and merge and how you can make use of them in your projects.

⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:

Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!

Join this channel to get access to perks:

Join my Discord server:

You like my free content? Here you can buy me a coffee:
Рекомендации по теме
Комментарии
Автор

Philipps' tutorials make programming so much easier and enjoyable

Alchemist
Автор

Thanks I needed a clean and quick explanation and demo of this for a code optimization. When ever I need android stuff I'd just search your name with the topic and probably you made a video about that!!! 👍👍👍

ernestguevara
Автор

Like this format, simple, clear, no digression

itsic-marking
Автор

I was struggling a little in my personal project due to having multiple flows where some need to wait for another to finish and you came with a solution I didn't know existed.

Thanks for another great video, Philipp.

Camlon
Автор

Thanks to this, I used combine to conveniently merge the results in the method of sequentially calling multiple suspend functions and merging the results. Thank you!

김정훈-ybp
Автор

Your video lecture is really helpful! Thanks a lot.

mikeshin
Автор

For combine we can use also something like this

val resultFlow: Flow<Boolean> = combine(
_flowOne,
_flowTwo,
_flowThree,
) { flowOne, flowTwo, flowThree ->
flowOne.length > 10 && flowTwo < 20 && flowThree
}.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = false,
)

Merge just accepts flows with the same type, whereas combine can accept flows with different types. Zip is more similar to combine than merge, hence people tend to think that merge is more similar to combine 😁

haykmkrtchyan
Автор

So combine will fire when one of two flows emits a new value

Zip will fire when both flows emit new values

Merge will listens to all flows that we pass to it and collect every value from any of them

abada-s
Автор

Another Gr8 gr8 gr8 toturial by phillip

miladhashemzadeh
Автор

Practical uses for Zip is when you're waiting for several asynchrounous functions to finish (such as web requests), and you only want to continue when all of them are done

TheMikkelet
Автор

Isn't it better to use combine(flow1, flow2, flow3...) syntaxis for 3 and more flows?

alekseyyakovlev
Автор

Though i don;t have time to watch yet, I come in to click the like button first🥰

jeckonly
Автор

There is also a standalone combine function that takes multiple flows, similar to merge. Just in case someone needs that.

damorpl
Автор

Best straightforward explanation out there. Thnx a lot :-)!

CivoMT
Автор

Hello Philipp, many thanks for the clear explanation. Top! Could you do video explain your prefered way to load the data model layer from a JSON file? This is so easy, and common on iOS. Did found a how to explanation for a Jetpack Compose project. Much appreciated. Best!

tmjromao
Автор

Hey Philip thanks for that, I was hoping you might show how to combine flows and use the result of the first flow as arguments to the combined flow. I had a situation where I wanted to do this and could not figure it out :( any ideas?

CitizenWarwick
Автор

So, technically, even though we are able to combine more than 2 flows, they won't run concurrently. RxJava's zip can combine upto 4 concurrent API calls...

Shriharshpathak
Автор

Hi Philipp! Love your videos. I found one issue with the approach you covered in this video and I am unclear about possible solutions. Maybe you could shed some light on it.

The issue is that when you launch the combine in viewModelScope it combines the flows asynchronously, however, there are many cases where you'd want to get a synchronous result of combining multiple flows. Here is a simple example:
* 5 text fields where user enters a number
* each of those fields has a mutable state containing a number entered
* there's a text field where you want to display a sum of all numbers entered.
When using combine().launchIn() approach the resulting sum will be displayed with an unnecessary delay caused by a launched coroutine. In this case, it would make sense to combine states synchronously. i.e once the state of an individual field is updated, the resulting state must be updated immediately.

A more relevant example might be having a draggable element on a screen. If you wanna use the dragging position to combine with other states. You could observe dragging state and resulting combined state separately in your view to avoid dragging delay. However it would be so much nicer to just observe 1 stateflow from ViewModel which would be the combined state.

Please let me know if you have an answer in your mind :)

alekseirozhnov
Автор

offtop but I cant find answer, Philipp, tell me please, if my screen contains bluetoothadapter, bluetooth callback etc where should I store it? If no fragments, just Activity with navhost. May be you will do some video with such a content, google don't know about how to do it, thanks in advance!

pumaelgatosiberian
Автор

in the example with combining three states can I use filter ? for example isAuthenticated.filter { it == true }.combine(user) { user -> user }.combine(posts) { user, posts -> .... }

MrDefiler