DEV Community

amay077
amay077

Posted on • Edited on

7 2

RxJava combineLatest quick example by Kotlin

RxJava's combineLatest is very convenient but I can use it difficultly because it has many overloads.
So I wrote the code snippet for combineLatest.

val name = PublishSubject.create<String>()
val age = PublishSubject.create<Int>()

// Can not omit Type parameters and BiFunction 
Observable.combineLatest<String, Int, String>(
        name, age, BiFunction { n, a -> "$n - age:${a}" })
        .subscribe({
            Log.d("combineLatest", "onNext - ${it}")
        })

// If you introduce RxKotlin then you can use type inference
Observables.combineLatest(name, age) { n, a -> "$n - age:${a}" }
        .subscribe({
            Log.d("combineLatest", "onNext - ${it}")
        })

// Also we can use Observable array for 1st parameter 
// but second parameter to be array, it's not cool.
Observable.combineLatest(arrayOf(name, age), {
    val n = it[0] as String
    val a = it[1] as Int
    "$n - age:${a}" })
        .subscribe({
            Log.d("combineLatest", "onNext - ${it}")
        })


name.onNext("saito")
age.onNext(24)
name.onNext("yoshida")
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

The countdown to March 31 is on.

Make the switch from app center suck less with Sentry.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️