Specifying a scheduler

To specify a scheduler on which a subscription is created, handler-executed, and disposed of, we use the subscribeOn operator. This is not normally used; instead, we just use ObserverOn, which specifies the scheduler events: next, error, and completed. The important thing to remember is that subscribeOn will direct which scheduler a subscription operates, whereas ObserveOn will only direct which scheduler to use after or below where ObserveOn is used in a chain. Let's look at this in a marble diagram first and then jump into a code example in the coming section.

We are starting on in the main thread, which is represented by the timeline here:

We will call ObserverOn and specify scheduler1, as shown:

Imagine that we ...

Get Reactive Programming with Swift 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.