显式类型参数Kotlin和RxJava

时间:2016-04-12 19:31:39

标签: rx-java kotlin

这是在Kotlin中使用RxJava的composeThreadTransformer的唯一可能方式吗?我只是不喜欢<MyType>函数中的compose部分。有可能省略吗?

  override fun call(): Observable<MyType> {
    return Observable.just(getData())
        .compose(threadTransformer.applySchedulers<MyType>())
  }

2 个答案:

答案 0 :(得分:21)

MAPIFolder inbox = Globals.ThisAddIn.Application.Session.GeteEfaultFolder(OlDefaultFolders.olFolderInbox) MAPIFulder subfolder = inbox.Folders["Subfolder Name"]; MailItem mail = Globals.ThisAddIn.Application.ActiveExplorer.Selection[1]; mail.Move(subfolder); 是Java中缺少扩展函数的一种解决方法。没有必要在Kotlin中使用Observable.compose,因为它支持开箱即用的扩展功能:

Observable.compose

答案 1 :(得分:2)

基于Vladimir Mironov的回答。根据您的需求进行扩展:

//Observable
fun <T> Observable<T>.applyIoScheduler() = applyScheduler(Schedulers.io())
fun <T> Observable<T>.applyComputationScheduler() = applyScheduler(Schedulers.computation())
private fun <T> Observable<T>.applyScheduler(scheduler: Scheduler) =
                subscribeOn(scheduler).observeOn(AndroidSchedulers.mainThread())

//Single
fun <T> Single<T>.applyIoScheduler() = applyScheduler(Schedulers.io())
fun <T> Single<T>.applyComputationScheduler() = applyScheduler(Schedulers.computation())
private fun <T> Single<T>.applyScheduler(scheduler: Scheduler) =
            subscribeOn(scheduler).observeOn(AndroidSchedulers.mainThread())

//Completable
//..