Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

java - What is the difference between RxJava 2 Cancellable and Disposable?

I want to create an Observable from view click listener using RxJava 2. I started from the simplest implementation (I don't use lambdas here to show you different types in this method):

 Observable<View> viewObservable = Observable.create(new ObservableOnSubscribe<View>() {
        @Override
        public void subscribe(@NonNull ObservableEmitter<View> e) throws Exception {
            mNewWordView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View value) {
                    if (!e.isDisposed()) {
                        e.onNext(value);
                    }
                }
            });
        }
    });

Then I thought about the way to set onClickListener to null if it is not needed further. I found that there are two methods with similar (as for me) names:

e.setCancellable(Cancellable c); and e.setDisposable(Disposable d);

What is the difference between them and which should I use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the Javadoc:

[Cancellable is] A functional interface that has a single cancel method that can throw.

The Disposable is not a functional interface plus when implementing its dispose() method, you are not allowed to throw checked exceptions.

In contrast, many non-RxJava components return a Closeable or AutoCloseable which are defined via throws IOException and throws Exception and are somewhat of a burden because you'd need try-catch it.

For example, you'd want to use setCancellable when you work with a file:

Observable.create((ObservableEmitter<byte[]> e) -> {
    FileInputStream fin = new FileInputStream("raw.dat");
    e.setCancellable(fin::close);

    byte[] buffer = new byte[4096];

    for (;;) {
        int r = fin.read(buffer);
        if (r < 0) {
            break;
        }
        e.onNext(buffer);
    }
    e.onComplete();
});

and you'd use setDisposable if you use a Scheduler:

Observable.create((ObservableEmitter<Event> e) -> {
    Worker worker = Schedulers.io().createWorker();
    e.setDisposable(worker);

    eventSource.onEvent(es ->
        worker.schedule(() -> e.onNext(es))
    );
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...