What is the difference between RxJava 2 Cancellable and Disposable?

后端 未结 2 868
攒了一身酷
攒了一身酷 2020-12-10 11:10

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 differ

相关标签:
2条回答
  • 2020-12-10 11:27

    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))
        );
    });
    
    0 讨论(0)
  • 2020-12-10 11:37

    The Cancellable ends up getting wrapped into a Disposable so the final effect is the same for either call. The difference is that Disposable has more features that you might not want to implement, so the simple Cancellable interface is there as an alternative.

    If you just want to dispose something when the observable ends use Cancellable. If you have a resource that might be disposed because of some external reason you want to implement Disposable to implement the Disposable.isDisposed() method.

    Do note that the methods are mutually exclusive. Only a single disposable or cancellable can be registered at once. Calling both overwrites the first one.

    0 讨论(0)
提交回复
热议问题