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

louiscad

06/11/2018, 12:49 PM
Hi, anyone is using the
select
expression? If yes, for what use cases?
d

deviant

06/11/2018, 2:05 PM
i use it when need a timeout on stream
Copy code
val hr = select {
                heartRateValueChannel.onReceive { it }
                onTimeout(10, TimeUnit.SECONDS, { -1 })
            }
m

minivac

06/11/2018, 2:36 PM
you should check go (golang) select samples for channels, they have very good use cases for a select
timeout is the most common
l

louiscad

06/11/2018, 2:38 PM
For timeout, I personally use
withTimeout
or
withTimeoutOrNull
b

bj0

06/11/2018, 9:27 PM
I use it for non-timeout purposes...
l

louiscad

06/11/2018, 10:41 PM
@bj0 Would you tell me your use case?
b

bj0

06/11/2018, 10:50 PM
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
4 Views