2016-11-26 85 views
1

大家好! 我有一些问题。我是RxJava/RxKotlin/RxAndroid的初学者,并且不了解某些功能。例如:如何用RxAndroid压缩Kotlin语言中的几个observables

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 ::转换的Android工作室告诉我接下来

enter image description here

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

回答

8

只是拉姆达替换功能参考:

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

而且不要忘记申报明确函数的返回类型:

fun getFrontData(): Observable<FrontDataModel> { 
    ... 
相关问题