dir.by  
  Поиск  
Компьютер, программы
Kotlin
 flow is the data flow in Kotlin. These are the substitution of LiveData in Android Kotlin and the substitution of Observable in RxJava 
посмотрели 808 раз
обновлено: 2 Augusta 2026
flow is an asynchronous data stream.
flow In other words, it's an asynchronous function that releases data.
flow is an alternative to standard collections (lists, arrays)
flow can only be used in coroutines
Example
  Kotlin  
fun main() {

     // The flow function describes a thread without starting
     val myflow1 = flow {
          emit(5) // thread releases the value
          emit(3) // thread releases the value
          emit(9) // thread releases the value
     }

     // The collect function starts the thread
     myflow1.collect { value ->  // Get data from the stream
           println( value)
     }
}
What is  flow  in a book?
flow is an implementation of the pattern observer. You have a data source and a subscriber, and flow implements the data source.
flow is a new part of coroutines introduced by JetBrains and designed to make coroutines a more reactive framework, replacing Rx and LiveData. Interestingly, when coroutines were invented, the advantages of coroutines were: convenient use and synchronous operation. That is, you can calculate all the data right where it is needed and callbacks are not needed.
But! After a long work with coroutines, it turned out that reactivity is still sometimes needed, but there was no suitable solution and they came up with flow.
There are several libraries and frameworks that provide similar capabilities for working with asynchronous data flows:
Shared Flow
Shared Flow stores a cache from which data is retrieved when a collector is subscribed to it.

Imagine that you are in an application for the selection of cars for sale. While the user minimizes the app, you have generated several new suggestions, and when the user opens the app, you can display the suggestions to them.

Moreover, using the parameters, you can filter out events, drop the first or last, and so on. If shared flow overflows, it will suspend the emitters until it has processed them, but it does not wait for the collectors to process them. That is, you get a very fast data bus inside your application.
MutableSharedFlow
MutableSharedFlow allows you to get data from both suspend sources and non-suspend sources at the same time.
You can also reset the cache in MutableSharedFlow if necessary.
StateFlow
StateFlow stores the last value from the data source.
If we received three values 5, 20, and 14, and the value 14 was the last value, then when we subscribe to StateFlow, we get the value 14.
Old analogues flow
RxJava (Observables, Observers)
RxJava is a library for reactive programming in Java, but is also available for Kotlin. It provides Observables and Observers for working with asynchronous data flows. RxJava has many operators for transforming, filtering, and merging data streams.
RxJava (Flowable)
The RxJava, Flowable is a more advanced version of Observable, which is designed to work with data flows, where there may be many elements and consumers may not have time to process the data as quickly as it arrives. This solves the problem of buffer overflows in asynchronous data streams.
Project Reactor
Project Reactor is a library for reactive programming in the Java language, which also has adapters for Kotlin. It provides Flux and Mono for working with asynchronous data, and also includes many operators for manipulating the data.
LiveData
LiveData This is the part of Android Architecture Components designed to work with Android components. It observes data changes and automatically updates the user interface when data changes.
Reactive Streams
Reactive Streams is a Java standard designed to work with asynchronous data streams. It defines the Publisher, Subscriber, and Subscription interfaces that can be implemented by various libraries such as RxJava, Project Reactor, and others.
 
← Previous topic
Generic, template class in Kotlin | Example: class MyBook[<]T[>] { ... }
 
Next topic →
The Kotlin console app works like Android Jetpack Compose
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is Kotlin?
Why is IntelliJ IDEA the most popular development environment for Kotlin?
Download and install IntelliJ IDEA to explore Kotlin
Create a new console project to explore Kotlin | IntelliJ IDEA
Setting the value
Declare the variable and set the value in Kotlin | val, var, lateinit, constant: const val, global variable: companion object
null
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
Int, Float, Boolean, Char ...
Integer numbers in Kotlin are: Byte, UByte, Short, UShort, Int, UInt, Long, ULong
Decimal numbers in Kotlin are: Float, Double
A flag with values of true or false in Kotlin is: Boolean
The symbol in Kotlin is: Char
Convert number to text in Kotlin | Int → String
String
The string, the text in Kotlin is: String
Interpolate strings in Kotlin. Example: val address:String = "${street}, ${country}"
What is the difference between String and StringBuilder ?
Enum
What is an enumeration (enum) in Kotlin ?
How do I find enum by value in Kotlin ?
Collections and Arrays
Collections are lists, dictionaries, and other classes for adding, removing items, searching for items, sorting items. | Kotlin
Lists in Kotlin. interface List , interface MutableList , class ArrayList
Create collections list, set, map and an array array in Kotlin
Function
fun() { ... } is a function in Kotlin. The function is needed to perform an action.
Extension function
Extension function in Kotlin. Allows you to add a new function to the system (List, ..) or my (Book, ...) class
Lambda function
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };
Class
What is class in Kotlin? Example: class MyBook { ... } | Primary and secondary constructor | block init
lateinit is a late initialization for the class field | Kotlin
class that inherits from interface in Kotlin | Example: class MyBook : IBook { ...}
Unnamed class that inherits from interface in Kotlin | Example: val book1 = object : IBook { ...}
Generic, template class in Kotlin | Example: class MyBook[<]T[>] { ... }
Flow
flow is the data flow in Kotlin. These are the substitution of LiveData in Android Kotlin and the substitution of Observable in RxJava
Writing a program
The Kotlin console app works like Android Jetpack Compose

  Ваши вопросы присылайте по почте: info@dir.by