Creating Observables
The following operators are used to create Observables from scratch, out of existing objects, arrays of other data structure, or by a sequence or timer.
The create operator
Creating Observables from scratch can be done by calling one of the following io.reactivex.Observable
methods (operators):
- Create
- Generate
- UnsafeCreate
The following example shows how to construct an Observable from scratch. Call onNext()
until the observer is not disposed, onComplete()
and onError()
programmatically in order to get a 1 to 4 range of numbers:
As we can see in the preceding screenshot, the output is as expected, range from 1 to 4, and the sequence gets disposed of after usage.
The defer operator
Creating a new Observable for each observer once the observer connects can be done by calling the defer
method. The following code shows the usage of defer
for the case when we supply a number:
The console print-line method outputs 123, which is the Observable-wrapped integer.
The empty operator
Creating...