i’m using MutableStateFlow to emit events but whil...
# coroutines
k
i’m using MutableStateFlow to emit events but while collecting these events i’m adding
delay
for 500ms but due to this i’m missing all the other data and only last TempData is recieved
Copy code
GlobalScope.launch() {
            flowMain
                .collect { data ->
                    Log.i("AppLogs", data.toString())
                    delay(1)
                }
        }
Copy code
viewInputLvlArray.forEachIndexed { index, view ->
    flowMain.value = TempData(index, someotherValue)
}
l
StateFlow only stores one value (state) so I guess you need to use something else like SharedFlow here ...
k
I have now tried SharedFlow, but in this i have tried
tryEmit()
that is not working but only
emit
is working
but with
emit
i need to run it in coroutine scope @Lukas Lechner
m
looks like you need Channel(Capacity.Unlimited) so you won’t suspend, and won’t lose events
l
Did you set the
replay
value to something like 15 in your SharedFlow?
k
i tried
replay
, it worked but problem is it worked only for first 15 items but list is iterated multiple times in some milliseconds @Lukas Lechner
r
try emit will fail if you don’t have a buffer.
I would just set the buffer size to 15, suspend on buffer flow and use emit
I think emit will try to do a tryEmit first before it uses a coroutine