Skip to content

Instantly share code, notes, and snippets.

@ShkurtiA
Created January 29, 2018 13:07
Show Gist options
  • Save ShkurtiA/58adf1401c4e6ac0b4774c688aaa3666 to your computer and use it in GitHub Desktop.
Save ShkurtiA/58adf1401c4e6ac0b4774c688aaa3666 to your computer and use it in GitHub Desktop.
Rxjava2 kotlin extension
fun <T> Flowable<T>.applySchedulersIO(): Flowable<T> {
return subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
}
fun <T> Flowable<T>.applySchedulersComputation(): Flowable<T> {
return subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread())
}
fun <T> Flowable<T>.subscribeUIToIO(onNext: (T) -> Unit, onError: (Throwable) -> Unit): Disposable {
return applySchedulersIO().subscribe({ next ->
onNext(next)
}, { error ->
onError(error)
})
}
fun <T> Flowable<T>.subscribeUIToComputation(onNext: (T) -> Unit, onError: (Throwable) -> Unit): Disposable {
return applySchedulersComputation().subscribe({ next ->
onNext(next)
}, { error ->
onError(error)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment