LiveDataToRxJava is a language extension library that provides some API for converting LiveData to RxJava.
With the kotlin extension function, you can easily convert LiveData
to RxJava
:
val completable = liveData.toCompletable()
val observable = liveData.toObservable()
val flowable = liveData.toFlowable()
val single = liveData.toSingle()
val maybe = liveData.toMaybe()
If you are using Java, Api will be slightly longer:
MutableLiveData<String> liveData = new MutableLiveData<>();
Completable completable = RxJavaConvert.toCompletable(liveData);
Observable<String> observable = RxJavaConvert.toObservable(liveData);
Flowable<String> flowable = RxJavaConvert.toFlowable(liveData);
Single<String> single = RxJavaConvert.toSingle(liveData);
Maybe<String> maybe = RxJavaConvert.toMaybe(liveData);
LiveData
can specify the period of observation in the lifecycle of a LifecycleOwner
. In our applications including Android support-26 library, the commonly used Activity
and Fragment
has implemented the LifecycleOwner
interface, so it can be very convenient to use our data in the lifecycle of UI.
The following code can be used to convert LifecycleOwner
into a Observable
:
//`this` can be an Activity or a Fragment
val observable: Observable<Lifecycle.Event> = LifecycleConvert.lifecycleObservable(this)
More often, you can obtain a observable with lifecycle:
//`this` can be an Activity or a Fragment
val observableWithLife = observable.bindLifecycle(this)
val singleWithLife = single.bindLifecycle(this)
val maybeWithLife = maybe.bindLifecycle(this)
val flowableWithLife = flowable.bindLifecycle(this)
val completableWithLife = completable.bindLifecycle(this)
The bindLifecycle
operator allows your observable to have the same lifecycle as LiveData.observe (this, {doSomething})
, that is, from onStart
state to onPause
state.
Binding Lifecycle Operators Not only Applies to
Observable
(or otherReactive
interfaces) converted fromLiveData
. So you can also use this library for otherRxJava
behavior binding lifecycle, such asRetrofit
network requests, which automaticallydispose
those requests when ui is no longer active.
There is a difference between LiveData
and Reactive
interface. LiveData
can pass null value, but the Reactive
interface can't. So the null value is awkward while converting to RxJava
. The toObservable
operator throws an exception when it encounters a null value. Here are a few of my suggestions for this:
- Define a constant or object that represents null, and use it instead.
- Use Java8
Optional
to wrap a nullable object. - Use
toObservableAllowNull
APIs
val observable = liveData.toObservableAllowNull(valueIfNull)
val single = liveData.toSingleAllowNull(valueIfNull)
val maybe = liveData.toMaybeAllowNull(valueIfNull)
val flowable = liveData.toFlowableAllowNull(valueIfNull)
val completable = liveData.toCompletableAllowNull()
LiveData
is non-thread-safe. The main methods, such as observe
, observeForever
, removeObserver
, setValue
, all need to be called on the main thread. This means that it is still necessary to subscribe in the main thread after the conversion into the Observable
(or other reactive
interface). This is regrettable but temporarily unavoidable.
These values will be ignored when Flowable
or Observable
emitts values outside the lifecycle. But for Single
, the only poor value, if ignored, is Maybe
:
val maybe = single.bindLifecycle(this)
And if you still want to operate on Single
, you can do this:
val singleWithLife = single.bindLifecycleWithError(this)
A CancellationException
will be thrown when values are emitted outside the lifecycle. Completable
is the same.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
compile 'com.github.YvesCheung:LiveDataToRxJava:v1.1'
}
Copyright 2018 Yves Cheung
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.