使用 LiveDataReactiveStreams
可以将LiveData
转换为Publisher
。
但是为了使用 RxJava2 方法,例如withLatestFrom
,我需要Observable
而不是Publisher
。是否有另一种方法将Publisher转换为Observable,旁边使用这个(过时的)库 - RxJava<->ReactiveStreams?
答案 0 :(得分:0)
// 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?) {}
})
答案 1 :(得分:-2)
所以我设法做了这样的事情。但我不知道将此代码放在android项目结构中的位置,因为toPublisher
需要Activity实例。
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)