如何使用RxAndroid用Kotlin语言压缩一些可观察对象 [英] How to zip few observables in Kotlin language with RxAndroid

查看:130
本文介绍了如何使用RxAndroid用Kotlin语言压缩一些可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我是RxJava/RxKotlin/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 {

    }
}

然后我使用 MainInteractor :: convert Android Studio接下来告诉我

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

我尝试了很多变体,试图理解它想要什么,但是没有成功.请帮助我...最好的问候.

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

推荐答案

只需将函数引用替换为lambda:

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> {
    ...

这篇关于如何使用RxAndroid用Kotlin语言压缩一些可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆