DEV Community

Discussion on: Binding Android UI with Kotlin Flow

Collapse
 
ijurcikova profile image
Ivet Jurčíková

Thanks for your effort and for making the library!

I have one, a bit unrelated question: while introducing safeOffer, you wrote: "But the function might throw an exception if the channel is closed for send." - When can this happen? How is it possible that the channel may be closed for send if its superior flow is not?

Collapse
 
ychescale9 profile image
Yang

Great question! You're right that in this case isClosedForSend should never return true in the callback, but this is only because in the the awaitClose block we've unregistered / removed the callback from the widget. So this is somewhat a "defensive" approach to make sure we don't crash if we forgot to unregister the callback (which is arguably worse as we usually want to fail early but that's a different conversation).

SendChannel.offer also has other issues (note the 3rd point), so even when isClosedForSend is false a subsequent offer call might still throw and hence in the safeOffer function we still need to wrap a try / catch around offer.

Hope that answered your question 😀