By using LiveDataReactiveStreams
one can transform LiveData
to Publisher
.
But in order to use RxJava2 method such as withLatestFrom
I need to have an Observable
instead of the Publisher
. Is there another way to convert Publisher to Observable beside using this (outdated) library - RxJava<->ReactiveStreams ?
2 Answers
// Flowable to LiveData
val flowable = Flowable.just(1, 2, 3)
val liveData = LiveDataReactiveStreams.fromPublisher<Int>(flowable)
// From Live Data to Publisher(Flowable is also publisher)
LiveDataReactiveStreams.toPublisher(this, liveData).subscribe(object: Subscriber<Int>{
override fun onNext(t: Int?) {}
override fun onComplete() {}
override fun onSubscribe(s: Subscription?) {}
override fun onError(t: Throwable?) {}
})
So I managed to do something like this. But I don't know where to put this code in android project structure, since toPublisher
requires Activity instance.
val rxDataPublisher: Publisher<DataSnapshot> = LiveDataReactiveStreams.toPublisher(this@LocationActivity,fireBaseLiveData)
val rxLocationPublisher: Publisher<Location> = LiveDataReactiveStreams.toPublisher(this@LocationActivity, locationLiveData)
val rxFlowable: Flowable<Pair<Location, DataSnapshot>> = Flowable.fromPublisher(rxDataPublisher)
.withLatestFrom(rxLocationPublisher, BiFunction{t1, t2 -> Pair(t2,t1) })
val flowableToLiveData = LiveDataReactiveStreams.fromPublisher(rxFlowable)
Flowable
is aPublisher
and can work withPublisher
s. You likely don't have to convert toObservable
.