Thanks to visit codestin.com
Credit goes to github.com

Skip to content

setCancellation for Single and Completable  #4356

@AntonHolovin

Description

@AntonHolovin

We can use setCancellation for the AsyncEmitter that we have inside Observable.fromAsync method. It's very useful because we are able to release something in the case when the subscription was terminated. For example:

Observable.fromAsync(emitter -> {
    SomeProtocolClient client = new SomeProtocolClient(getUri());

    Callback callback = new Callback() {
        @Override
        public void onLoad(Response response) {
            Model model = parse(response);

            emitter.onNext(model);
            emitter.onCompleted();
        }

        @Override
        public void onError() {
            emitter.onError(new ApiException("No response"));
        }
    };

    Request request = client.get(callback);
    emitter.setCancellation(request::cancel); // release some resources, it's useful to have setCalcellation
}, AsyncEmitter.BackpressureMode.BUFFER);

Wouldn't it be nice to have something like that for the Single and Completable?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions