If I use `asFlow()` on a closed `ConflatedBroadcas...
# coroutines
d
If I use
asFlow()
on a closed
ConflatedBroadcastChannel
will it receive the latest element sent before it was closed, or will the flow be empty?
e
It will be empty
d
So if I need to ensure that I receive that latest element, I'd have to not close that channel... then the flow would never end... is there any better way?
e
Why would you need the channel to be a broadcast one? What do you use it for?
d
I need to download files in one part of the android app (in a service), but I need to let the UI know of the state of those downloads. So I have nested `ConflatedBroadcastChannel`s one to manage the downloads, the other each download's progress (there are other things to be done after the download). There are different states (some files aren't ready to be downloaded or aren't compatible/available), and the UI needs to display this accordingly.
The progress works fine, but I think the other things get sent before the UI has a chance to do
asFlow()
, so if I close the channel, it won't receive them... if I don't the UI will keep on listening to an invalid channel...
e
Why whould you need to ever close them?
d
The inner channels are just temporary (while the service is running, they get sent using the outer channel), for each one, we
launch
a coroutine to listen to the changes it will report... wouldn't those coroutines just pile up and just waste memory as new ones come in that are more relevant?
Each item in the recyclerview listens to such flows to update its content accordingly...
e
If you launch just one coroutine to listen for changes then I don't see why would you need a broadcast channel. Just use a regular channel.
d
I need to listen to that both to update the recyclerview and the notification.
Each one has it's own class and is managed in a different module
e
Ok. Then you can avoid closing them, anyway. As long as your views will stop subscribing to those broadcast channels when no longer show, they will garbage-collected when no views remain showing them. You don't have to close them to make them eligible for GC.
d
If I use
cancel(someException)
or
close(someException)
will a
catch { }
on the
Flow
resulting from
asFlow()
catch it, or will it throw (since anyways those emissions are really a final state anyways...)?
If so, I could just use such a
Copy code
fun getDownloadListener() = downloadBroadcastChannel.asFlow().catch {
    when(it) {
        is ExceptionOne -> emit(StateOne())
        ....
    }
}
and reuse that ...
Even keeping the channel open, I still don't receive those emissions for some reason otherwise...
The progress is reported fine though...
Originally I thought it was because the progress gets sent when the channel is still open, but when I send those, the channel is closed by the time the UI starts listening... but that doesn't seem to be the case.