Search code examples
javaandroidkotlinrx-javarx-kotlin

Extending Rx Singles zip infinitely


I need to make a lot of API calls asynchronously and obviously make sure that they all pass so I can handle error/success in a single place.

Is there a way to easily extend Singles.zip() functionality in Kotlin to take more than base 9 parameters (in best case scenario, to make it take any given number of parameters) without doing massive amounts of copy-paste work when writing your own extensions for t10, t11, etc. ?

Well, writing dozens of extensions simply works, but is cumbersome, not very elegant and adds additional work.


Solution

  • Single.zip method takes an Iterable of Singles

    val list = arrayListOf<Single<String>>()
    list.add(Single.just("hello"))
    list.add(Single.just("world"))
    
    Single.zip(list) { args -> Arrays.asList(args) }
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.computation())
            .subscribe()