view model

PHOTO EMBED

Tue Jun 07 2022 18:40:48 GMT+0000 (UTC)

Saved by @mueeza #kotlin

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)
   }
}
content_copyCOPY