Flowable的onErrorResumeNext,networkOnMainThread出错

Mic*_*eap 11 android rx-java2

我有以下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
    }
Run Code Online (Sandbox Code Playgroud)

简而言之,我正在从本地数据库(一个地方)检索一些数据,我想将它与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
        }


    }
Run Code Online (Sandbox Code Playgroud)

但我想说明用户没有最后位置的scneario,因此该行:

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...")
                    }
Run Code Online (Sandbox Code Playgroud)

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

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)

    }
Run Code Online (Sandbox Code Playgroud)

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

万一你想知道,schedulerProvider.io()转换为:

Schedulers.io()
Run Code Online (Sandbox Code Playgroud)

GetPlace:

  /**
     * 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)
                }
    }
Run Code Online (Sandbox Code Playgroud)

Nic*_*oso 4

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

使用 Rx Schedulers 类中的 IO、New Thread 或 Computation 调度程序:

subscribeOn(Schedulers.computation())
Run Code Online (Sandbox Code Playgroud)

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

Thread.currentThread().getName()
Run Code Online (Sandbox Code Playgroud)

Schedulers.trampoline()如果您有不同的调度程序用于观察和订阅(如示例所示),这对于跟踪使用以下任一者时发生的情况特别有用:

.subscribeOn(schedulerProvider.io()).observeOn(schedulerProvider.ui())
Run Code Online (Sandbox Code Playgroud)