Table of Contents
RxJava Disposable
Observable 을 폐기해야 할때 Disposable
을 생성한다.
다수의 Observable 을 한번에 폐기하고 싶을 때는 CompositeDisposable 을 생성한다.
Disposable
Observable source = Observable.interval(1, TimeUnit.SECONDS);
Disposable disposable = source.subscribe(System.out::println);
new Thread(() -> {
try {
Thread.sleep(3500);
} catch(Exception e) {
e.printStackTrace();
}
disposable.dispose();
}).start();
CompositeDisposable
Observable source = Observable.interval(1, TimeUnit.SECONDS);
Disposable disposable1 = source.subscribe(System.out::println);
Disposable disposable2 = source.subscribe(System.out::println);
Disposable disposable3 = source.subscribe(System.out::println);
CompositeDisposable compositeDisposable = new CompositeDisposable();
// add(Disposable) or addAll(Disposable...)
compositeDisposable.add(disposable1);
compositeDisposable.addAll(disposable2, disposable3);
compositeDisposable.dispose();