Hi, anyone is using the `select` expression? If ye...
# coroutines
l
Hi, anyone is using the
select
expression? If yes, for what use cases?
d
i use it when need a timeout on stream
Copy code
val hr = select {
                heartRateValueChannel.onReceive { it }
                onTimeout(10, TimeUnit.SECONDS, { -1 })
            }
m
you should check go (golang) select samples for channels, they have very good use cases for a select
timeout is the most common
l
For timeout, I personally use
withTimeout
or
withTimeoutOrNull
b
I use it for non-timeout purposes...
l
@bj0 Would you tell me your use case?
b
hmm, the simplest one is I have a channel providing
Status
updates from a connection object, but I can also push my own custom
Status
objects. I use an
actor
looping on
select
to combine those two sources into a single
Status
channel
👍 1
the other use case is I have a protocol that encodes/decodes data. Instead of dealing with connections directly it has an in and out channels, and I select on these channels because the connection can only deal with one command/response at a time