Creating a Flowable
Earlier in this book, we used Observable.create()
a handful of times to create our own Observable
from scratch, which describes how to emit items when it is subscribed to, as shown in the following code snippet:
import io.reactivex.Observable; import io.reactivex.schedulers.Schedulers; public class Launcher { public static void main(String[] args) { Observable<Integer> source = Observable.create(emitter -> { for (int i=0; i<=1000; i++) { if (emitter.isDisposed()) return; emitter.onNext(i); } emitter.onComplete(); }); source.observeOn(Schedulers.io()) .subscribe(System.out::println); sleep(1000); } }
The output is as follows:
0 1 2 3 4 ...
This Observable.create()
will emit the integers 0 to 1000 and then call onComplete()
. It can be stopped abruptly if dispose()
is called on the Disposable
returned from subscribe...