Flow के Basics
val flow = flow {
emit(value) // produce values one by one
}
flow.map { it transformation }
.filter { condition }
.collect { value -> /* runs the flow */ }
Creating a Flow
flow { } एक cold stream बनाता है जो emit() का उपयोग करके एक-एक करके values emit करता है; जब तक flow collect नहीं होता तब तक अंदर कुछ नहीं चलता।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Creating a Flow
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun numberFlow(): Flow<Int> = flow {
for (i in 1..3) {
delay(20)
emit(i)
}
}
fun main() = runBlocking {
numberFlow().collect { value ->
println("Collected: $value")
}
}
Login to try C/C++/Java/PHP code in the editor
Flows Are Cold
कोई Flow values produce करना तब तक शुरू नहीं करता जब तक इस पर असल में .collect { } (या कोई दूसरा terminal operator) call न हो, यानी एक ही flow को कई बार collect किया जा सकता है, हर बार इसके logic को ताज़ा चलाते हुए।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Flows Are Cold
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun greetingFlow(): Flow<String> = flow {
println("Flow started")
emit("Hello")
}
fun main() = runBlocking {
println("Before collecting")
val flow = greetingFlow()
println("Flow created, but not started yet")
flow.collect { println(it) }
}
Login to try C/C++/Java/PHP code in the editor
Transforming a Flow
.map { } और .filter { } जैसे operators किसी Flow पर लगभग वैसे ही काम करते हैं जैसे किसी collection पर, एक pipeline बनाते हुए जो flow आखिरकार collect होने पर चलती है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Transforming a Flow
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..5).asFlow()
.filter { it % 2 == 0 }
.map { it * 10 }
.collect { println("Value: $it") }
}
Login to try C/C++/Java/PHP code in the editor
Flow vs a Single Suspend Result
जबकि एक साधारण suspend fun बिल्कुल एक value return करता है, एक Flow समय के साथ आने वाले शून्य, एक, या कई values represent करने के लिए बना है, इसे चल रहे data की streams के लिए बेहतर fit बनाते हुए।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Flow vs a Single Suspend Result
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val total = (1..5).asFlow().fold(0) { acc, value -> acc + value }
println("Total from flow: $total")
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि कोई
Flowcold है -- इसका code तब तक बिल्कुल नहीं चलता जब तक असल में कोई इसे.collect { }से collect न करे। - किसी
Flowके collector के अंदर suspend-friendly operations के बजाय block करना, जो Flow उपयोग करने का मकसद हरा सकता है। Flow(संभावित रूप से समय के साथ कई asynchronous values के लिए बना) को एक single value return करने वालेsuspend fun, याSequence(synchronous, suspend-aware नहीं) के साथ confuse करना।
flow { }values की एक cold asynchronous stream बनाता है, जोemit()से एक-एक करके emit होती है।- कोई
Flowतब तक कुछ नहीं करता जब तक इस पर.collect { }call न हो -- यह lazily execute होता है,Sequenceजैसी भावना में। .map { }और.filter { }जैसे Flow operators stream को transform करते हैं, collection operations के समान लेकिन suspend-aware।- Flows
kotlinx.coroutinesद्वारा प्रदान की जाती हैं और आमतौर पर समय के साथ आने वाले data की streams के लिए उपयोग होती हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: