Syam Sundar K
Syam Sundar K

Reputation: 129

How to Listen to more than Three Fields in Observables.combineLatest

I have code to listen to exactly three fields using Observables.combineLatest

Observables.combineLatest(text_name.asObservable(),text_username.asObservable(), text_email.asObservable()).subscribe({ t ->
            if (t.first.toString() != currentName || t.second.toString() != currentUsername) {
                startActionMode()
            } else {
                finishActionMode()
            }
        })

but when I add another parameter to the Observables.combineLatest it throughs error since only 3 inline-parameters can be passed..

Now I would wish to pass 4 parameters in the parameter list for Observables.combineLatest.. I know it should be done using an array or a list, passed in as parameter but It's hard for me to figure it out using Kotlin.

Help me out.. Thanks in Advance..

Upvotes: 4

Views: 2453

Answers (1)

Eoin
Eoin

Reputation: 4066

You need to add a combine function if you want to combine more than 3 observables. You can do something like this.

    Observables.combineLatest(
            first.asObservable(),
            second.asObservable(),
            third.asObservable(),
            forth.asObservable()
    )
    // combine function
    { first, second, third, forth->
        // verify data and return a boolean
        return@subscribe first.toString() != currentName || second.toString() != currentUsername
    }
    .subscribe({ isValid->
                   if (isValid) {
                       startActionMode()
                   } else {
                       finishActionMode()
                   }
               })

In the combine function you can verify your data and return a boolean. Then in subscribe you can take an action based on that boolean

Upvotes: 2

Related Questions