Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Forked from rubixhacker/RxLiveData.kt
Created September 28, 2018 10:39
Show Gist options
  • Save cesarferreira/e5c0cbed166e0d5e0a68be0a6353cc8d to your computer and use it in GitHub Desktop.
Save cesarferreira/e5c0cbed166e0d5e0a68be0a6353cc8d to your computer and use it in GitHub Desktop.
Kotlin extension functions to convert LiveData to and from a Flowable
// Add the following to your build.gradle file
// implementation "android.arch.lifecycle:reactivestreams:1.0.0-beta1"
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.LiveDataReactiveStreams
import io.reactivex.Flowable
/**
* Converts LiveData into a Flowable
*/
fun <T> LiveData<T>.toFlowable(lifecycleOwner: LifecycleOwner) : Flowable<T> =
Flowable.fromPublisher(LiveDataReactiveStreams.toPublisher(lifecycleOwner, this))
/**
* Converts a Flowable into LiveData
*/
fun <T> Flowable<T>.toLiveData(): LiveData<T> = LiveDataReactiveStreams.fromPublisher(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment