class viewModel() : ViewModel() { private val _sharedFlow = MutableSharedFlow<Int>() // Mutable Share Flow val sharedFlow = _sharedFlow.asSharedFlow() // Immutable Share Flow fun squareNumber(number:Int){ viewModelScope.launch { _sharedFlow.emit(number * number) } } init { viewModelScope.launch { sharedFlow.collect { delay(2000L) println("First Flow : The received Number is $it") } } viewModelScope.launch { sharedFlow.collect { delay(3000L) println("Secound Flow : The received Number is $it") } } squareNumber(3) } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter