Flow vs SharedFlow

what is Flow?

Flow is an interface , its just emit value it doesn't store any value on screen rotation it will be started from first. we use flow if you want to emit any api response to view or any multiple emit need to be done.

Example of Flow

fun onSendddata(): Flow<String> {

return flow{
repeat(5)
{
emit("Item $it")
delay(1000)
}

}
}

And collecting that response inside view using corroutine scope

lifecycleScope.laaunch{
viewModel.onJoinRoom().collectLatest {
binding.tvFlow.text = it
}
}

what is SharedFlow?

SharedFlow is also a flow it doesnt keep a value with it so only in below example we are not passing any value to it for the variaable _onJoinEvent.They will send event if they are no collectors for them.Mostly sharedflow are used to send one time event it will send the same value if already sent by it not like stateflow.It doesnt emit the last value on screen rotation but in stateflow it will be updated with last value.

private val _onJoinEvent = MutableSharedFlow<String>()
val onJoinEvent = _onJoinEvent.asSharedFlow()
_onJoinEvent.emit("Testing")

And inside view we can call

viewModel.onJoinEvent().collectLatest {

}

We use flow when we want to emit multiple things and for one time event its good to use sharedflow.