chansek
06/05/2021, 5:37 AMchannel.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?
suspend fun fetch(): Result<List<User>> {
collectFlow {
if (...)
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
?Erik
06/05/2021, 6:41 AMoffer
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
.