【问题标题】:How to zip few observables in Kotlin language with RxAndroid如何使用 RxAndroid 压缩 Kotlin 语言中的一些 observables
【发布时间】:2016-11-26 12:39:42
【问题描述】:

我有一些问题。我是 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 studio 告诉我下一个

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

【问题讨论】:

    标签: android rx-java kotlin rx-android rx-kotlin


    【解决方案1】:

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

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

    别忘了显式声明函数的返回类型:

    fun getFrontData(): Observable<FrontDataModel> {
        ...
    

    【讨论】:

      【解决方案2】:

      您还可以在 lambda Like 中显式指定 Function3 类型:

      Observable.zip(cats,
                     day,
                     top,
                     Function3<List<Product>, Product, List<Category>, FrontDataModel> 
                               { cats, day, top -> convert(cats, day, top) }
      

      并使用 IntelliJ idea 快捷方式alt+enter 来显示更多动作并更改高阶函数的显示格式。

      为什么选择 Function3?

      按照功能接口,如果它有两个输入参数,它是 BiFunction,如果它有 3 个输入,它是 Function3,并且列表还在继续。

      【讨论】:

        【解决方案3】:

        此代码适用于 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
                        }
                        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-05
          相关资源
          最近更新 更多