I'm working with rxjava2.
My problem is that sometimes server sends me nothing (response body is null, and so on List size = 0), so in that case I'd like to repeat request after 5 sec.
I have an Retrofit2 request:
@GET("/ics/api/{bidId}/calltracking/reports/widgets")
Single<List<CallTrackingWidget>> getWidgets(@Path("bidId") int bidId);
Use it like so:
RetrofitFactory.getRetrofitService().getWidgets(mDataManager.getBidId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(widgets -> getView().showLoading())
.doOnSuccess(widgets -> getView().hideLoading())
.repeatWhen(widgets -> widgets.flatMap(size -> {
if ((int) size == 0) {
return Flowable.just("asd").delay(5, TimeUnit.SECONDS);
} else {
return widgets;
}
}))
.subscribe(widgets -> {
mDataManager.getProduct().getCallTrackingData().setWidgets(widgets);
getView().initWidgets(mDataManager.getProduct().getCallTrackingData().getWidgetNames());
}, throwable -> {
handleError(throwable, R.string.error_internet);
})
But my code a have an exeption:
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
Help me please to repeat request after 5 sec, when List size is 0.
getView()tells me you probably are. You can only work with the UI using the main thread, in Android. - dazitodelay()operator will useSchedulers.computation()as its default scheduler. TherepeatWhen()will then move the whole observer chain on to a computation thread and out of the main thread. - Bob DalgleishrepeatWhen()operator up the chain so it occurs beforeobserveOn(). Anything downstream ofobserveOn()will continue to operate on the main thread, and upstream may change as needed. - Bob Dalgleish