0
votes

I am using RxJava combineLatest method in Kotlin with more than 3 Observables .I am getting error in implementing the method

val userNameObservable = RxHelper.getTextWatcherObservable(username)
    val passwordObservable = RxHelper.getTextWatcherObservable(password)

    var submitButtonObservable: Observable<String> ?=null
    button.setOnClickListener { submitButtonObservable = RxHelper.getButtonClickable() }

    observable = Observable.combineLatest(userNameObservable, passwordObservable, submitButtonObservable,Triple(userNameObservable,passwordObservable,submitButtonObservable))
    observable?.debounce(800, TimeUnit.MILLISECONDS)?.observeOn(AndroidSchedulers.mainThread())?.subscribe {

        if (username.text.isEmpty() || password.text.isEmpty())
            button.isEnabled = false
        else
            button.isEnabled = true
    }
}

the error i am getting is:

None of the following functions can be called with the arguments supplied: @CheckReturnValue @SchedulerSupport public final fun combineLatest(p0: ((Observer) -> Unit)!, p1: ((Observer) -> Unit)!, p2: ((Observer) -> Unit)!, p3: ((???, ???, ???) -> Unit)!): Observable! defined in io.reactivex.Observable @CheckReturnValue @SchedulerSupport public open fun combineLatest(p0: ObservableSource!, p1: ObservableSource!, p2: ObservableSource!, p3: io.reactivex.functions.Function3!): Observable! defined in io.reactivex.Observable @CheckReturnValue @SchedulerSupport public open fun combineLatest(p0: Function!, out Unit!>!, p1: Int, vararg p2: ObservableSource!): Observable! defined in io.reactivex.Observable

1
Please post the errorVadim Kotov

1 Answers

2
votes

There are three options:

Use Function3:

Observable.combineLatest(userNameObservable, passwordObservable, submitButtonObservable, Function3 { username: String, password: String, submitBtn: Any ->
        if (username.text.isEmpty() || password.text.isEmpty())
            button.isEnabled = false
        else
            button.isEnabled = true
    })

Use Observables.combineLatest:

    observable = Observables.combineLatest(userNameObservable, passwordObservable, submitButtonObservable)
    observable?.debounce(800, TimeUnit.MILLISECONDS)?.observeOn(AndroidSchedulers.mainThread())?.subscribe {
        //Access data from observables with it.first, it.second and it.third
    }

Use the Observable.combineLatest in its list form. Example:

Observable.combineLatest(listOf(TextViewObs.create(tietPassword), TextViewObs.create(tietPasswordRepeat))) {
            return@combineLatest it[0]==it[1]
        }.subscribe {
            if(it){
                //Passwords match
                tietPasswordRepeat?.error = null
            }else{
                //Passwords do not match
                tietPasswordRepeat?.error = getString(R.string.Passwords_must_match)
            }
        }