Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is close() and channel.close() the same for channelFlow (or callbackFlow)?

I have a callbackFlow below

fun <T> Flow<T>.callbackMerge(other: Flow<T>): Flow<T> = callbackFlow {
    channel.close()
    launch {
        collect { trySend(it) }
        channel.close()
    }
    other.collect { trySend(it) }
    awaitClose { println("Close Callback") }
}

I can use channel.close() or close(). They seem to behave the same to me.

Is there a different between them? When should I use which?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The docs of channel property say:

A reference to the channel this coroutine sends elements to. It is
provided for convenience, so that the code in the coroutine can refer
to the channel as channel as opposed to this. All the SendChannel
functions on this interface delegate to the channel instance returned by this property.

So basically they are the same due to delegation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading