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

chansek

06/05/2021, 5:37 AM
channel.offer
is now deprecated. Can anybody please help me understanding the
trySend
? I can see 3 flags like
isSuccess
,
isFailure
and
isClosed
. When should I use what?
Copy code
suspend fun fetch(): Result<List<User>> {
    collectFlow {
        if (...)
Copy code
offer(Result.Success(listOf(...)))
        else
        offer(Result.Error(Exception())
    }
}
How this can be converted into
trySend
? Shall I use,
trySend(Result.Success...).isSuccess
or
trySend(Result.Error...).isSuccess
. When should I use
trySend...isFailure
and
trySend...isCancel
?
e

Erik

06/05/2021, 6:41 AM
It depends on how you were using
offer
before. And it depends on how you want to send items to the channel. • Just want to try and offer a value? Then just
trySend
• Also want to know if sent?
isSuccess
is what you need. • Want to handle a failure to send? Then use either of the other options. If you are in a suspending context, you might want to
send
instead of
trySend
.
92 Views