RxJava - zip list of Observable

前端 未结 3 621
Happy的楠姐
Happy的楠姐 2021-02-13 02:28

I have a list of Observables (RxJava 1).

List observableList = new ArrayList<>();

It can contain at least 1 Observable.

3条回答
  •  滥情空心
    2021-02-13 02:55

    ReactiveX - Zip operator

    Zip beyond BiFunction

    Zip combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function

    Here, list is an Array List of Observables of whichever type you want to pass.

    val list = arrayListOf>()
    
    
    Observable.zip(list) { args -> Arrays.asList(args) }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({
            val response = it[0]
            val imageUrlResponse = imageUrlObject as ImageUrlResponse
            urls.add(imageUrlResponse.imageUrl)}
                   }, {
            val c = it
     })
    

    The Result of the following subscription is this image below. Just like we expect it to be zipped together. Also can you notice it returns all the responses to be zipped in a single java.lang.Object[].

    Note I had to type cast my Array List to access my single object because it is of type Any!

提交回复
热议问题