Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Forked from rubixhacker/RxLiveData.kt
Created August 3, 2018 07:13
Show Gist options
  • Save aoudiamoncef/ef7804abdba930a4c358e40cf47af7d6 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/ef7804abdba930a4c358e40cf47af7d6 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