How to zip few observables in Kotlin language with RxAndroid
Asked Answered
A

3

11

I have some problem. I'm a beginer in RxJava/RxKotlin/RxAndroid, and dont understand some features. For Example:

import rus.pifpaf.client.data.catalog.models.Category
import rus.pifpaf.client.data.main.MainRepository
import rus.pifpaf.client.data.main.models.FrontDataModel
import rus.pifpaf.client.data.product.models.Product
import rx.Observable
import rx.Single
import rx.lang.kotlin.observable
import java.util.*


class MainInteractor {

    private var repository: MainRepository = MainRepository()

    fun getFrontData() {

        val cats = getCategories()
        val day = getDayProduct()
        val top = getTopProducts()

        return Observable.zip(cats, day, top, MainInteractor::convert)
    }

    private fun getTopProducts(): Observable<List<Product>> {
        return repository.getTop()
                .toObservable()
                .onErrorReturn{throwable -> ArrayList() }

    }

    private fun getDayProduct(): Observable<Product> {
        return repository.getSingleProduct()
                .toObservable()
                .onErrorReturn{throwable -> Product()}

    }

    private fun getCategories(): Observable<List<Category>> {
        return repository.getCategories()
                .toObservable()
                .onErrorReturn{throwable -> ArrayList() }
    }

    private fun convert(cats: List<Category>, product: Product, top: List<Product>): FrontDataModel {

    }
}

Then I'm use MainInteractor::convert Android studio tell me next

enter image description here

I tried a lot of variant and tried to understand what does it want, but no success. Help me please... Best Regards.

Aimo answered 26/11, 2016 at 12:39 Comment(0)
D
22

Just replace function reference with lambda:

return Observable.zip(cats, day, top, { c, d, t -> convert(c, d, t) })

And don't forget to declare function's return type explicitly:

fun getFrontData(): Observable<FrontDataModel> {
    ...
Drye answered 26/11, 2016 at 13:55 Comment(0)
P
9

You can also explicitly specify the Function3 type in the lambda Like :

Observable.zip(cats,
               day,
               top,
               Function3<List<Product>, Product, List<Category>, FrontDataModel> 
                         { cats, day, top -> convert(cats, day, top) }

and play with the IntelliJ idea shortcut alt+enter to display more actions and change the displaying format of the high-order function.

enter image description here

Why Function3?

Following The Functional Interfaces if it has two Input params it's a BiFunction and if it has 3 Inputs it's a Function3 and the list goes on.

Pose answered 20/4, 2019 at 20:43 Comment(0)
S
-1

This code worked for me with RXJAVA2

    for (i in dbCharacters) {
           // LoadImageToDBS(getApplication<Application>().applicationContext, i, this)
            //   charList.characterList[i.id!!].imageRawData = dbCharacters[i.id!!].imageRawData

            observable =
                    fetcher.loadImage(i.url)
                            .doOnSubscribe {
                                disposable.add(it)
                            }

            singleImageList.add(observable)

            observable.subscribe(object : DisposableSingleObserver<Bitmap>() {
                override fun onSuccess(t: Bitmap) {
                    Log.d("Image", "onSuccess")
                    i.imageRawData = bitMapToString(t)
                    updateCharacter(i)
                }

                override fun onError(e: Throwable) {
                    Log.d("Image Loading Error",e.message)
                }

            })
      }

        Single.zip(singleImageList, { args -> Arrays.asList(args) })
                .subscribe(
                        {
                            Log.d("Zip", "Zip Success")
                            countryLoadError.value = false
                            loading.value = false
                        },{
                    val c = it
                }
                )
Shockley answered 21/11, 2019 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.