Explain the difference between Cold Flow and Hot Flow with an example.
Cold Flow π₯Ά
β‘οΈ A cold flow is a data flow that produces values only when there is an active collector.
β‘οΈ Each collector receives the emitted values independently, and they start receiving emissions from the beginning when they subscribe.
β‘οΈ If we subscribe to a cold flow, we will receive all of the emitted values, even if the flow has already completed emitting values.
β When to use?
β‘οΈ Cold flows are typically used for tasks that need to be performed only once, such as loading data from a database or fetching data from a remote server.
β‘οΈ Example
Flow (itself)
Cold Flow π₯Ά
β‘οΈ A cold flow is a data flow that produces values only when there is an active collector.
β‘οΈ Each collector receives the emitted values independently, and they start receiving emissions from the beginning when they subscribe.
β‘οΈ If we subscribe to a cold flow, we will receive all of the emitted values, even if the flow has already completed emitting values.
β When to use?
β‘οΈ Cold flows are typically used for tasks that need to be performed only once, such as loading data from a database or fetching data from a remote server.
β‘οΈ Example
Flow (itself)
Hot Flow π₯
β‘οΈ A hot flow is a data flow that emits values regardless of whether there are active collectors. It can produce values even if there are no subscribers.
β‘οΈ New collectors joining a hot flow might miss emissions that occurred before they started listening.
β‘οΈ If we subscribe to a hot flow, we will start receiving emissions from the point where the flow is currently at. If the flow has already completed emitting values, we will not receive any emissions.
β When to use?
β‘οΈ Hot flows are typically used for tasks that need to be performed continuously, such as updating a Ul with real-time data.
β Example
StateFlow and SharedFlow
β‘οΈ A hot flow is a data flow that emits values regardless of whether there are active collectors. It can produce values even if there are no subscribers.
β‘οΈ New collectors joining a hot flow might miss emissions that occurred before they started listening.
β‘οΈ If we subscribe to a hot flow, we will start receiving emissions from the point where the flow is currently at. If the flow has already completed emitting values, we will not receive any emissions.
β When to use?
β‘οΈ Hot flows are typically used for tasks that need to be performed continuously, such as updating a Ul with real-time data.
β Example
StateFlow and SharedFlow
How to observe lifecycle events (onCreate(), onResume(), onStart(),
etc)
Owner CompositionLocal can provide the current LifecycleOwner.
etc)
Owner CompositionLocal can provide the current LifecycleOwner.
You can use SubComposeLayout to measure and resize Composables.
Here we have a username field and a button that enables when the username is valid.
It starts off empty and so our state is false. Now when the user starts typing, our state correctly updates and our button becomes enabled.
This is where derivedStateOf comes in. Our state is changing more than we need our UI to update and so derivedStateOf can be used for this to reduce the number of recompositions.
It acts similarly to the Kotlin Flows distinctUntilChanged() operator.
It starts off empty and so our state is false. Now when the user starts typing, our state correctly updates and our button becomes enabled.
This is where derivedStateOf comes in. Our state is changing more than we need our UI to update and so derivedStateOf can be used for this to reduce the number of recompositions.
It acts similarly to the Kotlin Flows distinctUntilChanged() operator.