4

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..

Zoe
  • 27,060
  • 21
  • 118
  • 148
Syam Sundar K
  • 129
  • 1
  • 12

1 Answers1

2

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

Eoin
  • 4,050
  • 2
  • 33
  • 43
  • **return@subscribe** shows an error.. but **return@combineLatest** works, but not correctly.. it initiates **startActionMode()** as soon as the activity is launched.. – Syam Sundar K Feb 23 '18 at 15:14