我正在尝试使我的代码更多Rx,所以这里是我在重构期间遇到的问题:
我以前的功能是这样的:
private fun startLocationUpdates() {
compositeDisposable.add(rxLocationRepository.onLocationUpdate()
.subscribe(Consumer { location ->
appendGeoEvent(location)
}))
rxScheduleStopLocationUpdates()
}
我把它变成这样的东西:
val locationUpdates = rxLocationRepository.onLocationUpdate()
.concatMap(appendGeoEvent())
.doOnSubscribe(rxScheduleStopLocationUpdates())
.publish()
private fun startLocationUpdates() {compositeDisposable.add(locationUpdates.connect()) }
现在问题是appendGeoEvent使用我创建的locationUpdates ConnectableObservable。
appendGeoEvent看起来像这样(它是两个连接的函数)
fun appendGeoEvent(location: Location) {
val locationGeoEvent = LocationGeoEvent(
accuracy = location.accuracy.toDouble(),
latitude = location.latitude,
longitude = location.longitude,
timestampGeoEvent = location.time
)
appendGeoEvent(locationGeoEvent)
}
private fun appendGeoEvent(event: LocationGeoEvent) {
synchronized(mEventsMonitor) {
if (!normalSaveScheduled.get()) {
configureNormalSaveCompletable()
}
locationEvents.add(event)
}
}
所以现在我需要让这两个函数中的一个函数返回一些Observable或Copmletable。 再次,如何使它有意义并使用此locationUpdates ConnectableObservable:
val locationUpdates = rxLocationRepository.onLocationUpdate()
.concatMap(appendGeoEvent())
.doOnSubscribe(rxScheduleStopLocationUpdates())
.publish()
代码的另一部分是:
1)configureNormalSaveCompletable方法:
fun configureNormalSaveCompletable() {
scheduleNormalSave.subscribe(Action { rxSaveEvents(locationEvents, false) })
}
2)scheduleNormalSave Completable(它计划将来做某事的动作并设置rxSaveEvents方法中所需的布尔值)
val scheduleNormalSave = Completable.timer(locationEventsSaveThrottlingSeconds, TimeUnit.SECONDS).doOnSubscribe(Consumer { normalSaveScheduled.set(true) }).doOnComplete(Action { normalSaveScheduled.set(false) })
3)这是rxSaveEvents方法:
private fun rxSaveEvents(
events: List<LocationEvent>,
locationUpdatesAboutToStop: Boolean): Boolean {
synchronized(mEventsMonitor) {
if (events.isNotEmpty()) {
val locationEvents = LocationEvents()
segregateLocationEvents(events, locationEvents)
scoreReadings(locationEvents)
sendLocationEventsInternal(locationEvents, locationUpdatesAboutToStop)
Completable.fromCallable { updateLocationEventsToDb(locationEvents) }.subscribeOn(Schedulers.io()).subscribe()
}
if (locationUpdatesAboutToStop) {
stopLocationUpdates()
Completable.fromCallable { cleanAllReadingsData() }.subscribeOn(Schedulers.io()).subscribe()
Completable.fromCallable { removeExpiredData() }.subscribeOn(Schedulers.io()).subscribe()
}
locationEvents.clear()
return true
}
}
更新
到目前为止我尝试过的是:
//what should be in the method parameter and how to exctract this
fun apendGeoEvent(location: Location): Observable<LocationGeoEvent>? {
return Observable.just(location)
.flatMap { t: Location -> Observable.just(LocationGeoEvent(
accuracy = location.accuracy.toDouble(),
latitude= location.latitude,
longitude = location.longitude,
timestampGeoEvent = location.time
)
)
}
.doOnNext{
if (!normalSaveScheduled.get()) {
configureNormalSaveCompletable()
}
locationEvents.add(it)
}
}
但是我不知道如何将Observable从上游带到我的appendGeoEvent方法:
val locationUpdates = rxLocationRepository.onLocationUpdate()
.concatMap(appendGeoEvent())
.doOnSubscribe(rxScheduleStopLocationUpdates())
.publish()