大家好! 我有一些问题。我是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 {
}
}
然后我用 MainInteractor :: convert Android工作室告诉我下一步
我尝试了很多变体并尝试了解它想要什么,但没有成功。请帮助我...最好的问候。
答案 0 :(得分:12)
只需用lambda替换函数引用:
return Observable.zip(cats, day, top, { c, d, t -> convert(c, d, t) })
并且不要忘记明确声明函数的返回类型:
fun getFrontData(): Observable<FrontDataModel> {
...
答案 1 :(得分:0)
您还可以在lambda Like中明确指定Function3类型:
Observable.zip(cats
,day
,top
,Function3<List<Product>, Product, List<Category>, FrontDataModel>
{ cats, day, top -> convert(cats, day, top) }
并使用IntelliJ主意快捷键alt+enter进行播放,以显示更多操作并更改高阶功能的显示格式。
为什么要使用Function3?
在功能接口之后,如果有两个输入参数,则为BiFunction,如果有3个输入,则为Function3,列表继续。
答案 2 :(得分:0)
此代码可用于RXJAVA2
for (i in dbCharacters) {
// LoadImageToDBS(getApplication<Application>().applicationContext, i, this)
// charList.characterList[i.id!!].imageRawData = dbCharacters[i.id!!].imageRawData
observable =
fetcher.loadImage(i.url)
.doOnSubscribe {
disposable.add(it)
}
singleImageList.add(observable)
observable.subscribe(object : DisposableSingleObserver<Bitmap>() {
override fun onSuccess(t: Bitmap) {
Log.d("Image", "onSuccess")
i.imageRawData = bitMapToString(t)
updateCharacter(i)
}
override fun onError(e: Throwable) {
Log.d("Image Loading Error",e.message)
}
})
}
Single.zip(singleImageList, { args -> Arrays.asList(args) })
.subscribe(
{
Log.d("Zip", "Zip Success")
countryLoadError.value = false
loading.value = false
},{
val c = it
}
)