Created
November 9, 2017 05:30
-
-
Save amay077/c99bb3cda5563a0a07f1cbf7ddedf8d3 to your computer and use it in GitHub Desktop.
Convert RxJava:Observable<T> to AAC:LiveData<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your.awesome.package | |
import android.arch.lifecycle.LiveData | |
import android.arch.lifecycle.MutableLiveData | |
import io.reactivex.Observable | |
import io.reactivex.disposables.Disposable | |
fun <T> Observable<T>.toLiveData() : LiveData<T> { | |
return object : MutableLiveData<T>() { | |
var disposable : Disposable? = null; | |
override fun onActive() { | |
super.onActive() | |
// Observable -> LiveData | |
disposable = [email protected]({ | |
this.postValue(it) | |
}) | |
} | |
override fun onInactive() { | |
disposable?.dispose(); | |
super.onInactive() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment