Ravin Jain
10/02/2021, 6:39 PMlouiscad
10/02/2021, 7:25 PMbuffer()
for these collectors. You might also want to use MutableSharedFlow
instead.Ravin Jain
10/02/2021, 7:30 PMlouiscad
10/02/2021, 7:35 PMRavin Jain
10/02/2021, 7:43 PMimport android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
class someviewmodel @Inject constructor() : ViewModel() {
private val _test = MutableSharedFlow<String>()
val test: SharedFlow<String>
get() = _test.asSharedFlow()
private fun check() {
someFun(
call1 = {
viewModelScope.launch {
_test.emit(it)
}
},
call2 = {
viewModelScope.launch {
_test.emit(it)
}
}
)
}
private fun someFun(call1: (s1: String) -> Unit, call2: (s2: String) -> Unit) {
call1("hello")
call2("morning")
}
fun loadCheck() {
check()
}
}
And this Screen in compose where i am collecting
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
@InternalCoroutinesApi
@Composable
fun SomeScreen(
viewModel: someviewmodel = hiltViewModel(),
) {
LaunchedEffect(true){
Log.d("Ravin", "LaunchedEffect")
viewModel.test.buffer().collect {
Log.d("Here", "here $it")
}
}
LaunchedEffect(Unit){
viewModel.loadCheck()
}
}
In the logs i dont see this log at all
Log.d("Here", "here $it")
Am i missing something herelouiscad
10/02/2021, 7:44 PMlouiscad
10/02/2021, 7:45 PM