Skip to content

Instantly share code, notes, and snippets.

@rubixhacker
Created September 28, 2017 04:11
Show Gist options
  • Save rubixhacker/d186e0727d4185ce6e46e1ef263faef8 to your computer and use it in GitHub Desktop.
Save rubixhacker/d186e0727d4185ce6e46e1ef263faef8 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