gpt4 book ai didi

android - Flowable 的 onErrorResumeNext、networkOnMainThread 出错

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:54 30 4
gpt4 key购买 nike

我有以下 rxJava 链:

 override fun combineLocationToPlace(req: Flowable<Place>): Flowable<Place> {
var combinedFlowable = Flowable
.combineLatest(
req,
getLastLocation().lastOrError().toFlowable(),
BiFunction<Place, Location, Place> { t1, location ->
Timber.w("FIRSTINIT - Retrieved location $location")
var placeLocation = Location(t1.placeName)
placeLocation.latitude = t1.latitude
placeLocation.longitude = t1.longitude
t1.distance = location.distanceTo(placeLocation)
t1
})


return combinedFlowable
.onErrorResumeNext { t: Throwable ->
Timber.w(t, "FIRSTINIT - Could not retrieve location for place (${t.message}) returning original request")
req
}
.doOnError {
Timber.w("FIRSTINIT - did detect the error here...")
}

return combinedFlowable
}

简而言之,我正在从本地数据库(一个地方)中检索一些数据,我想将它与来自 GPS 的最新位置结合起来:

 override fun getLastLocation(requestIfEmpty: Boolean): Observable<Location> {
var lastLocation = locationProvider.lastKnownLocation
.doOnNext {
Timber.w("Got location $it from last one")
}
.doOnComplete {
Timber.w("did i get a location?")
}

if (requestIfEmpty) {
Timber.w("Switching to request of location")
lastLocation = lastLocation.switchIfEmpty(requestLocation())
}

return lastLocation.doOnNext {
Timber.w("Got something!")
location = it
}


}

但我想考虑用户没有最后位置的场景,因此该行:

return combinedFlowable
.onErrorResumeNext { t: Throwable ->
Timber.w(t, "FIRSTINIT - Could not retrieve location for place (${t.message}) returning original request")
req
}
.doOnError {
Timber.w("FIRSTINIT - did detect the error here...")
}

它试图捕获任何错误并仅使用原始请求重试,而不将其与任何内容组合。我这样调用这段代码:

fun getPlace(placeId: String) {
locationManager.combineLocationToPlace(placesRepository.getPlace(placeId))
.onErrorResumeNext { t: Throwable ->
Timber.e(t, "Error resuming next! ")
placesRepository.getPlace(placeId)
}.subscribeOn(schedulerProvider.io()).observeOn(schedulerProvider.ui())
.subscribeBy(
onNext = {
place.value = Result.success(it)
},
onError = {
Timber.e("ERROR! $it")
place.value = Result.failure(it)
}
)
.addTo(disposables)

}

但是,当没有位置时抛出 NoSuchElementException,我的 flowable 切换到原始请求,然后在执行它时我得到一个 NetworkOnMainThread 异常。这个请求不应该在我放在那里的 scheduler.io() 上执行吗(因为我把代码放在那之前)?

如果您想知道,schedulerProvider.io() 转换为:

Schedulers.io()

获取地点:

  /**
* Retrieves a single place from database
*/
override fun getPlace(id: String): Flowable<Place> {
return Flowable.merge(placesDao.getPlace(id),
refreshPlace(id).toFlowable())
}

/**
* Triggers a refreshPlace update on the db, useful when changing stuff associated with the place
* itself indirectly (e.g. an experience)
*/
private fun refreshPlace(id: String): Single<Place> {
return from(placesApi.getPlace(id))
.doOnSuccess {
placesDao.savePlace(it)
}
}

最佳答案

为确保您的网络发生在主线程之外,将其显式发送到后台线程。

使用 Rx 调度程序类中的 IO、新线程或计算调度程序:

subscribeOn(Schedulers.computation())

如果您不想这样做(或者如果您认为它应该已经在后台线程上并且只想调试),您可以按如下方式记录线程信息:

Thread.currentThread().getName()

当您使用 Schedulers.trampoline() 或者您有不同的观察和订阅调度程序时,这对于跟踪正在发生的事情特别有用,就像您的示例一样:

.subscribeOn(schedulerProvider.io()).observeOn(schedulerProvider.ui())

关于android - Flowable 的 onErrorResumeNext、networkOnMainThread 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51421834/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com