2016-11-26 86 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> { 
    ... 
相關問題