3

I know how to do this in RxJava 2.

And I know how RxKotlin helps with similar issues.

But it seems that RxKotlin.Observables doesn't have this helper function for the list overload and I cannot figure it out. How would you do this?

Herrbert74
  • 2,578
  • 31
  • 51

3 Answers3

6

Most static functions in RxJava are extension functions in RxKotlin. This particular function is an extension on Iterable<Observable<T>>. You can call it like this:

listOfObservables.combineLatest { ... }
marstran
  • 26,413
  • 5
  • 61
  • 67
6

for RxJava 2 this could be done in this way

    val list = Arrays.asList(
            remoteRepository.getHospitals(),
            remoteRepository.getQuestionCategories(),
            remoteRepository.getQuestions(),

    )


    return Observable.combineLatest(list) {

        val hospitals = it[0] as List<Hospital>

        val questionCategories = it[1] as List<QuestionCategory>
        val questions = it[2] as List<Question>


        localRepository.insertHospitals(hospitals)
        localRepository.insertQuestionCategories(questionCategories)
        localRepository.insertQuestions(questions)


        if (hospitals.isNotEmpty())
            Constants.STATUS_OK
        else
            Constants.STATUS_ERROR

    }
alireza rahmaty
  • 576
  • 6
  • 16
1
val list = Arrays.asList(Observable.just(1), Observable.just("2"))
Observable.combineLatest(list, object : FuncN<String>() {
    fun call(vararg args: Any): String {
        var concat = ""
        for (value in args) {
            if (value is Int) {
                concat += value
            } else if (value is String) {
                concat += value
            }
        }
        return concat
    }
})

Observable.just(1), Observable.just("2") can be replaced with list of observable and login inside call fun will also changed as per requirements.

fdermishin
  • 3,519
  • 3
  • 24
  • 45
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38