Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip few observables in Kotlin language with RxAndroid

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.

like image 323
Scrobot Avatar asked Nov 26 '16 12:11

Scrobot


People also ask

How Observable Zip works?

Zip operator is used to combine emission from multiple observable into a single observable. Zip Operator Works in Sequence. That means each observable will be executed sequentially. So as we see the output source2 only emits item after source1 has emitted all items.

What is Observable zip?

The Zip method returns an Observable that applies a function of your choosing to the combination of items emitted, in sequence, by two (or more) other Observables, with the results of this function becoming the items emitted by the returned Observable.

What is Reactivex Kotlin?

Kotlin Extensions for RxJava RxKotlin is a lightweight library that adds convenient extension functions to RxJava. You can use RxJava with Kotlin out-of-the-box, but Kotlin has language features (such as extension functions) that can streamline usage of RxJava even more.

What is RX Observable?

There are two key types to understand when working with Rx: Observable represents any object that can get data from a data source and whose state may be of interest in a way that other objects may register an interest. An observer is any object that wishes to be notified when the state of another object changes.


2 Answers

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.

like image 54
Bachiri Taoufiq Abderrahman Avatar answered Oct 30 '22 21:10

Bachiri Taoufiq Abderrahman


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> {
    ...
like image 45
Maksim Ostrovidov Avatar answered Oct 30 '22 19:10

Maksim Ostrovidov