What is the use of flow. In which case it can be u...
# android
v
What is the use of flow. In which case it can be use any suggestions for this ?
g
Flow : For a start, Publisher Subscriber pattern / Reactive pattern.
v
Any examples ?
g
Codelabs from Android Developer site could give you an idea 🙂
t
In my opinion flow is not really good fit for subscriber pattern. For this you should look at Channels. And flow is more elaborated version of Sequences. So it is a flow of data you can process. One usage could be loading a big list of data from the web. And you want to display the first data before everything is downloaded. And also you only want to download data as needed. This could be implemented using flow very easyly
🚫 1
Here is a sample how to implement paging using flow:
Copy code
data class Data(val data: String)
val dataFlow<Flow<Data>> = pagingFlow(firstPage = 1) { page ->
    loadPageData() // must return a list of Data
}

fun <T>pagingFlow(firstPage: Int = 0, maxPages: Int = 100, init: (suspend FlowCollector<T>.() -> Unit)? = null, loadPage: suspend (Int) -> List<T>): Flow<T> {
    var page = firstPage
    return flow {
        init?.invoke(this)
        var emittedValues = true
        while (page < maxPages && emittedValues) {
            emittedValues = false
            loadPage(page).forEach {
                emit(it)
                emittedValues = true
            }
            page++
        }
    }
}
g
Doesn't the publisher subscriber pattern include the component (buffer or channel) in its built in form when talking about patterns ? Sequence to me is stream inside the channel and flow is just not the stream but inclusive of the channel as well.
t
Flow can also handle exceptions. So in case of an exception e.g. you could reload the page
g
Apologies @Vivek Modi for not giving an example as I am away from the system and most importantly code labs would give a step wise understanding of the concept.
t
And Flow is cold. So it will not start doing anything until you consume the flow.
g
Hot or cold does not matter as you have higher order functions to manipulate the stream. I.E. replay. Am I incorrect in my understanding ?
t
Sequences are also kind of cold. But with flow you have much more control and there you have coroutine support included and much more.
g
And Flow is cold. So it will not start doing anything until you consume the flow.
There are hot flows as well
The biggest difference bewtween Sequences and Flows is that Sequences are purely sequential (which implies from name), it cannot be used for any async operations, so it makes it impossible to use except blocking data processing
@Vivek Modi Flow is Kotlin implementation of concept of Reactive Streams, there is a bunch of blogs/books/articles about it. In general it’s a pattern which allows to work with any asyncronous streams of data and supports back pressure mechanism (so it solves problem when subscriber is slower than publisher). It used in great amount of use cases like UI (because all UI interactions can be represented as combination of asyncronous event, from user or from code), for IO (especially nice with any streams of data like sockets, bluetooth etc) and anything else which can be represented as a stream of events
🙏 1
v
thanks alll