Check out the rest of the series:
Knowing your RxJava Operator Toolbelt 2: Transforming Observables
Knowing your RxJava Operator Toolbelt 3: Combining Observables
Knowing your RxJava Operator Toolbelt 4: Filtering Observables
Knowing your RxJava Operator Toolbelt 5: Utility Observables

So you’ve just read an introduction to RxJava (hopefully mine) and now you’re ready to get your hands dirty. You start creating observing then eventually it strikes you: you need to start chaining your observables, combining them, probably even just getting your data into observables.

Knowing your observables is only the first part. RxJava has a fairly high skill cap, and how close you get to it is determined by how well you know your operators, so this is the beginning of a series that explores the arsenal of tools you have at your disposal.

The first category of these operators come in the form of creating observables. In my introduction (and most people’s), we used Observable.just and Observable.range. These are great for testing stuff out and teaching the concept, but both of them just emit objects that are already there so, not much practical use there. That’s where we begin with our first operator:

Create

This is the bare bones observable. You write what happens when an observer subscribes and what data the (provided) emitter emits, as well as when it completes

onSubscribe this emits [1]

Defer

Creates a new observable instance each time an observer subscribes. As such, you pass in another observable into defer. You could see it like create but with one key difference that makes it run the function inside the observable again for each new subscriber instead of just running it once and emitting the same data to each new subscriber.

Still can’t get your head around it? The create observable above will emit [1, 1, 1] if three subscribers subscribed to it. a++ is ran only once. The defer above will emit [1, 2, 3] because a++ is ran every time it is subscribed to.

From

Creates an observable from any iterable. This can include any array, list, or collection of documents in the case of Firestore. Data is emitted after each iteration (so the collection can still be being looped through while data is being emitted).

onSubscribe this emits [1, 2, 3, 4, 5]

Just

 

Our favourite testing observable. Just pass in up to 10 objects of any type and they will be emitted

onSubscribe this just emits [1, 2, 3]

Range

Our second favourite testing observable  Just pass in a range of values and they will be emitted.

onSubscribe this emits [0, 1, 2, 3, 4]

Repeat

Add this to any observable to make it emit its data sequence repeatedly. Pass in a number to make it repeat that many times, otherwise it’s infinite.

onSubscribe this emits [1, 2, 3, 1, 2, 3, 1, 2, 3…]

Timer

Emits [0] after a set delay. Just [0]. I don’t know if it’s meant to be literally just [0], but it’s just [0]. Use it as a timer.

For more dynamic delayed data, you can use delay.

Delay

Add this to any observable to make it delay its first emission.

onSubscribe this emits [1, 2, 3] together after 5 seconds.

Interval

At a set interval, this emits a sequence of integers starting from 0 (emits the number of times it’s emitted) and goes on infinitely.

Conclusion

By no means am I an expert at RxJava. I hardly even know about these operators before I write about them here, but that’s the beauty of talking about it, innit? To really absorb each section of RxJava’s wide array of operators, we’ll take it slowly and steadily.