Knowing your RxJava Operator Toolbelt 1: Creating Observables

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:


var a = 0
val observable = Observable.create<Int> { emitter ->

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]


var a = 0
val observable = Observable.defer {
    Observable.create<Int> { emitter ->

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.


val numbers = arrayOf(1, 2, 3, 4, 5)
val observable = Observable.fromArray(numbers)

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]


val observable = Observable.just(1, 2, 3)

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]


val observable = Observable.range(0, 5)

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]


val observable = Observable
        .just(1, 2, 3)

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…]


val observable = Observable.timer(5, TimeUnit.SECONDS)

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.


val observable = Observable
        .just(1, 2, 3)
        .delay(5, TimeUnit.SECONDS)

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

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


val observable = Observable.interval(2, TimeUnit.SECONDS)

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.


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.