Scr*_*bot 9 android kotlin rx-java rx-android rx-kotlin
大家!我有一些问题.我是RxJava/RxKotlin/RxAndroid的初学者,并且不了解一些功能.例如:
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 {
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用MainInteractor :: convert Android studio告诉我接下来
我尝试了很多变体并试图了解它想要什么,但没有成功.请帮助我...最好的问候.
Mak*_*dov 17
只需用lambda替换函数引用:
return Observable.zip(cats, day, top, { c, d, t -> convert(c, d, t) })
Run Code Online (Sandbox Code Playgroud)
并且不要忘记明确声明函数的返回类型:
fun getFrontData(): Observable<FrontDataModel> {
...
Run Code Online (Sandbox Code Playgroud)
您还可以在 lambda Like 中显式指定 Function3 类型:
Observable.zip(cats,
day,
top,
Function3<List<Product>, Product, List<Category>, FrontDataModel>
{ cats, day, top -> convert(cats, day, top) }
Run Code Online (Sandbox Code Playgroud)
并使用 IntelliJ idea 快捷键alt+enter显示更多动作并更改高阶函数的显示格式。
为什么是函数 3?
遵循功能接口,如果它有两个输入参数,它是一个BiFunction,如果它有 3 个输入,它是一个Function3,列表继续。
| 归档时间: |
|
| 查看次数: |
10878 次 |
| 最近记录: |