https://kotlinlang.org logo
Title
s

Simon Lin

08/31/2020, 9:48 AM
infix fun <E, R> ReceiveChannel<E>.zip(
    other: ReceiveChannel<R>
): ReceiveChannel<Pair<E, R>> (source)
is deprecated. What is the alternative?
I would like to zip two upload progress listener into one. Any advice?
val avatarProgressChannel = Channel<Int>()
val thumbnailProgressChannel = Channel<Int>()

avatarTask.addOnProgressListener { progress ->
  avatarProgressChannel.offer(progress) // 0, 1, 2, ..., 100
}

thumbnailTask.addOnProgressListener { progress ->
  thumbnailProgressChannel.offer(progress) // 0, 1, 2, ..., 100
}

avatarProgressChannel.zip(thumbnailProgressChannel) { p1, p2 -> p1 + p2 }
  .consumeAsFlow()
  .asLiveData() // expose to UI layer, emit 0, 1, 2, ..., 200
e

ephemient

08/31/2020, 10:00 AM
convert to flows and use
fun <T1, T2, R> Flow<T1>.zip(other: Flow<T2>, transform: suspend (T1, T2) -> R): Flow<R>
s

Simon Lin

08/31/2020, 10:05 AM
like this?
avatarProgressChannel.receiveAsFlow()
        .zip(thumbnailProgressChannel.receiveAsFlow()) { p1, p2 -> p1 + p2 }
        .asLiveData()
e

ephemient

08/31/2020, 10:12 AM
documentation says: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/zip.html
Deprecated: Channel operators are deprecated in favour of Flow and will be removed in 1.4