https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

Kulwinder Singh

07/12/2022, 11:10 AM
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

Lukas Lechner

07/12/2022, 11:17 AM
StateFlow only stores one value (state) so I guess you need to use something else like SharedFlow here ...
k

Kulwinder Singh

07/12/2022, 11:33 AM
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

myanmarking

07/12/2022, 12:22 PM
looks like you need Channel(Capacity.Unlimited) so you won’t suspend, and won’t lose events
l

Lukas Lechner

07/12/2022, 12:22 PM
Did you set the
replay
value to something like 15 in your SharedFlow?
k

Kulwinder Singh

07/13/2022, 5:32 PM
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

Raheel Naz [Amex]

07/23/2022, 12:22 AM
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
3 Views