Hi.
I have a library which returns observables. And I have another one which require to return the data in a synchronous way.
Particularly, I’m talking about OkHttp Interceptors. I need to retrieve the oauth token in order to add it as header. But this data comes from an observable.
public class TwitterInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest = request.newBuilder()
.addHeader("oauth_token", RxToken.getTwitterToken().toBlocking().first())
.build();
return chain.proceed(newRequest);
}
}
Calling toBlocking().first() is the only way I can think to solve this problem. But I do not know if calling toBlocking() may have some unexpected effects (I mean I know that this observable resolves its task reading from disk or memory, so it is not a really heavy task). But because it seems to be not recommended to use it in production code, as a general rule.
Thanks.
Hi.
I have a library which returns observables. And I have another one which require to return the data in a synchronous way.
Particularly, I’m talking about OkHttp Interceptors. I need to retrieve the oauth token in order to add it as header. But this data comes from an observable.
Calling
toBlocking().first()is the only way I can think to solve this problem. But I do not know if callingtoBlocking()may have some unexpected effects (I mean I know that this observable resolves its task reading from disk or memory, so it is not a really heavy task). But because it seems to be not recommended to use it in production code, as a general rule.Thanks.