Skip to content

Calling to Observable.toBlocking(). Always a bad practice? #3956

@VictorAlbertos

Description

@VictorAlbertos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions